【问题标题】:refer to global data from a template function?从模板函数中引用全局数据?
【发布时间】:2016-08-08 18:41:28
【问题描述】:

模板通常是内联的 - 您必须在声明中提供定义。

全局(静态)数据要求数据只有一个定义(但可以多次声明)。

因此,对于具有静态数据的类,通常在类定义(头文件)中声明静态,并在实现文件(.cpp)中将存储声明为静态。

但是对于需要引用静态/全局数据的模板该怎么办?

这里有一些代码可以给你一些具体的考虑:

        // we represent in a formal manner anything that can be encoded in a MSVS format specification
        // A format specification, which consists of optional and required fields, has the following form:
        // %[flags][width][.precision][{h | l | ll | w | I | I32 | I64}] type
        // based on https://msdn.microsoft.com/en-us/library/56e442dc.aspx
        struct FormatSpec
        {
            enum Size {
                normal,
                h,
                l,
                ll,
                w,
                I,
                I32,
                I64
            };

            enum Type {
                invalid,
                character,
                signed_integer,
                unsigned_integer,
                unsigned_octal,
                unsigned_hex,
                floating_point,
                expontential_floating_point,
                engineering_floating_point,
                hex_double_floating_point,
                pointer,
                string,
                z_string
            };

            unsigned    fLeftAlign : 1;
            unsigned    fAlwaysSigned : 1;
            unsigned    fLeadingZeros : 1;
            unsigned    fBlankPadding : 1;
            unsigned    fBasePrefix : 1;

            unsigned    width;
            unsigned    precision;

            Size        size_;
            Type        type_;
        };

        struct FormatSpecTypeDatum
        {
            FormatSpec::Type    id;         // id
            const TCHAR *       symbol;     // text symbol
        };

        FormatSpecTypeDatum kTypeSpecs[] =
        {
            { FormatSpec::character, _T("c") },
            { FormatSpec::character, _T("C") },
            { FormatSpec::signed_integer, _T("d") },
            { FormatSpec::signed_integer, _T("i") },
            { FormatSpec::unsigned_octal, _T("o") },
            { FormatSpec::unsigned_integer, _T("u") },
            { FormatSpec::unsigned_hex, _T("x") },
            { FormatSpec::unsigned_hex, _T("X") },
            { FormatSpec::expontential_floating_point, _T("e") },
            { FormatSpec::expontential_floating_point, _T("E") },
            { FormatSpec::floating_point, _T("f") },
            { FormatSpec::floating_point, _T("F") },
            { FormatSpec::engineering_floating_point, _T("g") },
            { FormatSpec::engineering_floating_point, _T("G") },
            { FormatSpec::hex_double_floating_point, _T("a") },
            { FormatSpec::hex_double_floating_point, _T("A") },
            { FormatSpec::pointer, _T("p") },
            { FormatSpec::string, _T("s") },
            { FormatSpec::string, _T("S") },
            { FormatSpec::z_string, _T("Z") },
        };

        template <typename ctype>
        bool DecodeFormatSpecType(const ctype * & format, FormatSpec & spec)
        {
            for (unsigned i = 0; i < countof(kTypeSpecs); ++i)
                if (format[0] == kTypeSpecs[i].symbol[0])
                {
                    spec.type_ = kTypeSpecs[i].id;
                    ++format;
                    return true;
                }
            return false;
        }

它相对简单 - 字符表示查找表的符号 ID。

我希望能够将 DecodeFormatSpecType() 用于 char、unsigned char、wchar_t 等。

我可以从 DecodeFormatSpecType() 中删除模板,只为各种字符类型提供重载接口。

主要的是数据并没有真正改变 - unsigned char 'c' 和 wchar_t 'c' 和 legacy char 'c' 具有完全相同的值,无论字符的存储大小如何(对于核心ASCII 字符是正确的,尽管毫无疑问还有其他一些编码(例如 EDBIC)不是正确的,这不是我要在这里解决的问题)。

我只是想了解“如何构建我的 C++ 库,以便我可以访问在一个位置定义的全局数据 - 它存储为一个数组 - 我希望访问模板化代码知道全局的长度数据,就像我可以使用普通的非模板化代码一样,拥有一个全局符号表,就像我在示例代码中展示的那样,表和需要其大小的实现都存在于适当的 .cpp 文件中”

这有意义吗?

全局数据 + 需要知道确切定义但也可以呈现(通过接口)这个通用(到有效域)的函数。

【问题讨论】:

  • 看起来我可以在 .cpp 文件中制作一组具体的表,并使与这些表一起工作的所有接口具体化,然后模板化我需要引用的任何函数对那些...

标签: c++ templates static


【解决方案1】:

函数模板可以毫无问题地使用全局函数和全局数据。

如果您想封装kTypeSpecs 的定义而不在头文件中定义它,您可以使用几个函数来提供对数据的访问。

size_t getNumberOfTypeSpecs();

// Provide read only access to the data.
FormatSpecTypeDatum const* getTypeSpecs();

然后将DecodeFormatSpecType实现为

template <typename ctype>
bool DecodeFormatSpecType(const ctype * & format, FormatSpec & spec)
{
   size_t num = getNumberOfTypeSpecs();
   FormatSpecTypeDatum const* typeSpecs = getTypeSpecs();

   for (unsigned i = 0; i < num; ++i)
      if (format[0] == typeSpecs[i].symbol[0])
      {
         spec.type_ = typeSpecs[i].id;
         ++format;
         return true;
      }
   return false;
}

getNumberOfTypeSpecsgetTypeSpecs 函数可以在 .cpp 文件中实现为:

// Make the data file scoped global variable.
static FormatSpecTypeDatum kTypeSpecs[] =
{
   { FormatSpec::character, _T("c") },
   { FormatSpec::character, _T("C") },
   { FormatSpec::signed_integer, _T("d") },
   { FormatSpec::signed_integer, _T("i") },
   { FormatSpec::unsigned_octal, _T("o") },
   { FormatSpec::unsigned_integer, _T("u") },
   { FormatSpec::unsigned_hex, _T("x") },
   { FormatSpec::unsigned_hex, _T("X") },
   { FormatSpec::expontential_floating_point, _T("e") },
   { FormatSpec::expontential_floating_point, _T("E") },
   { FormatSpec::floating_point, _T("f") },
   { FormatSpec::floating_point, _T("F") },
   { FormatSpec::engineering_floating_point, _T("g") },
   { FormatSpec::engineering_floating_point, _T("G") },
   { FormatSpec::hex_double_floating_point, _T("a") },
   { FormatSpec::hex_double_floating_point, _T("A") },
   { FormatSpec::pointer, _T("p") },
   { FormatSpec::string, _T("s") },
   { FormatSpec::string, _T("S") },
   { FormatSpec::z_string, _T("Z") },
};

size_t getNumberOfTypeSpecs()
{
   return sizeof(kTypeSpecs)/sizeof(kTypeSpecs[0]);
}

FormatSpecTypeDatum const* getTypeSpecs()
{
   return kTypeSpecs;
}

更新,以回应 OP 的评论

是的,你可以。以下是完全有效的:

size_t getNumberOfTypeSpecs()
{
   static constexpr size_t num = sizeof(kTypeSpecs)/sizeof(kTypeSpecs[0]);
   return num;
}

constexpr size_t getNumberOfTypeSpecs()
{
   return sizeof(kTypeSpecs)/sizeof(kTypeSpecs[0]);
}

【讨论】:

  • 我认为没有任何方法可以将表格的长度提取为constexpr?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-01-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-02-26
相关资源
最近更新 更多