如何初始化具有类型特征的类模板的静态数据成员
How can I initialize a static data member of a class template with type traits?
我试着使用这样的东西,但初始化似乎不起作用。当我删除类型特征时,它就会按预期工作。
template<typename _T, typename = std::enable_if_t<std::is_integral<_T>::value>>
struct Foo
{
static int bar;
};
template<typename _T>
int Foo<_T>::bar = 0;
如何正确初始化这样一个静态变量?
这是因为您使用std::enable_if
不太正确。
template<typename T, typename = std::enable_if_t<std::is_integral<T>::value>>
struct Foo;
template <typename T>
struct Foo<T, false> //Partial specialization
{
// if T is not integral, Foo becomes empty class
};
template <typename T>
struct Foo<T, true> //Partial specialization
{
static int bar;
};
然后:
template<typename T>
int Foo<T, true>::bar = 0;
我将_T
更改为T
,因为定义为_X
、__X
或__x
的名称是为内部实现保留的。
相关文章:
- 静态数据成员的问题-修复链接错误会导致编译器错误
- 静态数据成员模板专用化的实例化点在哪里
- 调用在 HXX 文件中声明的静态数据成员
- 虚拟成员函数的定义是否强制在同一转换单元中动态初始化静态数据成员?
- 错误: 无效使用非静态数据成员"应用程序::应用程序构造函数"
- 静态数据成员:它"const declaration / constexpr definition"起作用?
- 何时需要定义类的静态数据成员 (un/-)
- 为什么静态数据成员不能在c++11中的类中初始化
- 静态数据成员的模板专用化
- 拒绝包含某些公共静态数据成员的类型
- GCC:在调试构建中优化的静态数据成员
- 类模板静态数据成员定义/声明/初始化
- C++ 中的静态数据成员
- 如果类在 C++ 中具有常量或引用类型的非静态数据成员,为什么编译器不提供默认赋值运算符?
- 使用 lambda 函数初始化静态数据成员
- 引用静态数据成员
- 学习C++并在早期示例中遇到错误(在非静态数据成员之前需要构造函数)
- Windows 上的 Clang/LLVM 7 和 8 多次初始化内联静态数据成员(使用 link.exe 和 lld-
- 为什么不能使用"( )"为类的非静态数据成员提供默认值?
- 不带初始值设定项的 constexpr 静态数据成员