Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/132.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 模板类中的静态常量类型_C++_Templates_Static Variables - Fatal编程技术网

C++ 模板类中的静态常量类型

C++ 模板类中的静态常量类型,c++,templates,static-variables,C++,Templates,Static Variables,我一直在研究这个问题,但还没有找到解决办法。基本上,我需要在模板类中初始化一个静态常量类型变量 class MyType { public: MyType (int a, int b) { } }; template <class T> class MyClass { public: static const MyType Type; }; 类MyType { 公众: MyType(inta,intb){} }; 模板 类MyClass { 公众: 静态常量MyT

我一直在研究这个问题,但还没有找到解决办法。基本上,我需要在模板类中初始化一个静态常量类型变量

class MyType
{
public:
    MyType (int a, int b) { }
};

template <class T>
class MyClass
{
public:
    static const MyType Type;
};
类MyType
{
公众:
MyType(inta,intb){}
};
模板
类MyClass
{
公众:
静态常量MyType;
};

在cpp内初始化类型将产生链接器错误。在标头内初始化类型将导致它被多次初始化。无法在类内初始化类型,因为它是非整数类型。如何在不限制类专门化的情况下解决这个问题。非常感谢您的帮助。

我不确定您所说的“在cpp中初始化类型将产生链接器错误”是什么意思。但是,假设您实际上是指定义,那么您一定是做错了什么,因为在适当的位置为每种类型定义静态成员肯定有效!类模板中包含的是对象的声明,如果引用过对象,则需要在某个地方定义该声明。只有当
MyType
恰好是一个整数类型时,您才可以在类[template]中初始化它,并且您永远不需要它的地址(例如,将它绑定到一个常量引用或获取它的地址),您就可以不定义它。这是因为在这种情况下,它始终被视为常量表达式

我猜您试图在某个cpp文件中定义对象,如下所示:

template <typename T> MyType const MyClass<T>::Type = some-initialization-here;
顺便说一句,我很确定这个问题以前已经得到了回答,肯定是在


在标头内初始化类型将导致它被多次初始化

当然可以。对于使用实例化的
MyClass
的每个不同类型,一次。对于每种类型,它也是一个不同的对象,这是模板工作的固有特性。如果只需要定义和初始化一次,请将其放在非模板库中:

namespace detail{
class MyClassBase{
protected:
  ~MyClassBase(){} // only usable as a base class, non-polymorphic
  static const MyType Type; // only available to derived types
};
} // detail::

template<class T>
class MyClass
  : private detail::MyClassBase // private, non-polymorphic
{
public:
  using MyClassBase::Type; // if you want to expose 'Type' to the public audience
};
在一个.cpp中,并完成它



请注意,通常最好将
static
对象封装在函数中,如@Dietmar所示。这些函数本地静态优于任何其他类型的静态对象,因为您在使用它们时不会遇到静态初始化顺序的失败。

“在头中初始化类型将导致它被多次初始化。”您能否给出一个具体的例子,说明您是如何做到这一点的?如果只是检查构造函数是否被多次调用,则应该多次调用,因为每个模板类实例化有一个成员。请注意,该函数仍然会导致
MyClass
包含多个
MyType
对象。除此之外,+1。函数局部静态优于类局部静态或全局静态,因为它们没有静态初始化顺序。@Xeo:好的,当然:每个实例化有一个对象。如果您不想这样做,您需要将静态成员考虑到一个公共的[non-templatized]基类中。
template <typename T>
MyType const& MyClass<T>::Type() {
    static MyType rc = some-initialization-here;
    return rc;
}
namespace detail{
class MyClassBase{
protected:
  ~MyClassBase(){} // only usable as a base class, non-polymorphic
  static const MyType Type; // only available to derived types
};
} // detail::

template<class T>
class MyClass
  : private detail::MyClassBase // private, non-polymorphic
{
public:
  using MyClassBase::Type; // if you want to expose 'Type' to the public audience
};
const MyType detail::MyClassBase::Type = /*initialize here*/;