在.h文件中定义的模板类静态成员变量应该发生什么 [英] What should happen to template class static member variables with definition in the .h file

查看:242
本文介绍了在.h文件中定义的模板类静态成员变量应该发生什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果模板类定义包含依赖于模板类型的静态成员变量,那么我不确定应该采取什么可靠的行为。

If a template class definition contains a static member variable that depends on the template type, I'm unsure of what the reliable behavior should be?

最好将静态成员的定义放在与类定义相同的.h文件中,因为

In my case it is desirable to place the definition of that static member in the same .h file as the class definition, since


  1. 我希望类对于许多模板数据类型,我目前没有
    知道。

  2. 我希望在每个给定的模板类型的整个程序中只共享
    的静态成员的一个实例。 (一个用于 MyClass< int> 和一个用于所有 MyClass< double> 等。 b $ b
  1. I want the class to be general for many template data types that I don't currently know.
  2. I want only one instance of the static member to be shared throughout my program for each given template type. ( one for all MyClass<int> and one for all MyClass<double>, etc.

我可以简要说明此链接中列出的代码 a>

I can be most brief by saying that the code listed at this link behaves exactly as I want when compiled with gcc 4.3. Is this behavior according to the C++ Standard so that I can rely on it when using other compilers?

这个链接是什么意思?这个链接是什么意思?不是我的代码,而是由 CodeMedic 发布到讨论这里。我发现了像这样的其他几个辩论 one ,但是我不认为是结论性的。

That link is not my code, but a counter example posted by CodeMedic to the discussion here. I've found several other debates like this one but nothing I consider conclusive.

我认为链接器合并多个发现的定义(在示例中 ao bo
这是必需的/可靠的链接程序行为吗?

I think the linker is consolidating the multiple definitions found ( in the example a.o and b.o ). Is this the required/reliable linker behavior?

推荐答案

从N3290,14.6:

From N3290, 14.6:


A [...]类模板的静态数据成员应该定义在
中每个翻译单元,它隐式实例化[...],除非相应的专门化明确实例化[...] 。

A [...] static data member of a class template shall be defined in every translation unit in which it is implicitly instantiated [...], unless the corresponding specialization is explicitly instantiated [...] .

通常,将静态成员定义与模板类定义一起放入头文件中:

Typically, you put the static member definition in the header file, along with the template class definition:

template <typename T>
class Foo
{
  static int n;                       // declaration
};

template <typename T> int Foo<T>::n;  // definition

要扩展特许权:如果您计划在代码中使用显式实例化, :

To expand on the concession: If you plan on using explicit instantiations in your code, like:

template <> int Foo<int>::n = 12;

那么你必须不要 c $ c> Foo 也用于除了包含显式实例化的TU之外的其他TU,因为您将得到多个定义。

then you must not put the templated definition in the header if Foo<int> is also used in other TUs other than the one containing the explicit instantiation, since you'd then get multiple definitions.

然而,如果你需要为所有可能的参数设置一个初始值,而不使用显式的实例化,你必须把它放在标题中,例如与TMP:

However, if you do need to set an initial value for all possible parameters without using explicit instantiation, you have to put that in the header, e.g. with TMP:

// in the header
template <typename T> int Foo<T>::n = GetInitialValue<T>::value;  // definition + initialization

这篇关于在.h文件中定义的模板类静态成员变量应该发生什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆