为什么不是成员变量的重复定义?
Why it is not duplicate definition of member variable?
我实现了一个有静态成员变量的模板,看了标准后,发现在.h文件中定义变量是正确的,但是.h文件包含在多个.cpp中,为什么静态变量不是多重定义?
标准中有任何参考吗?
N3376 中的 14.5.1.3
A definition for a static data member may be provided in a namespace
scope enclosing the definition of the static member’s class template.
[Example:
template<class T> class X { static T s; };
template<class T> T X<T>::s = 0;
<-------------Question here.
—end example ]
因为它是一个模板。当您实例化静态成员时,它将被定义,但允许在多个翻译单元中定义隐式实例化。
[basic.def.odr]/6
There can be more than one definition of a class type (Clause 9), enumeration type (7.2), inline function with external linkage (7.1.2), class template (Clause 14), non-static function template (14.5.6), static data member
of a class template (14.5.1.3), member function of a class template (14.5.1.1), or template specialization for
which some template parameters are not specified (14.7, 14.5.5) in a program provided that each definition
appears in a different translation unit, and provided the definitions satisfy the following requirements.
我实现了一个有静态成员变量的模板,看了标准后,发现在.h文件中定义变量是正确的,但是.h文件包含在多个.cpp中,为什么静态变量不是多重定义?
标准中有任何参考吗?
N3376 中的 14.5.1.3
A definition for a static data member may be provided in a namespace scope enclosing the definition of the static member’s class template. [Example:
template<class T> class X { static T s; };
template<class T> T X<T>::s = 0;
<-------------Question here.—end example ]
因为它是一个模板。当您实例化静态成员时,它将被定义,但允许在多个翻译单元中定义隐式实例化。
[basic.def.odr]/6
There can be more than one definition of a class type (Clause 9), enumeration type (7.2), inline function with external linkage (7.1.2), class template (Clause 14), non-static function template (14.5.6), static data member of a class template (14.5.1.3), member function of a class template (14.5.1.1), or template specialization for which some template parameters are not specified (14.7, 14.5.5) in a program provided that each definition appears in a different translation unit, and provided the definitions satisfy the following requirements.