如何正确地特化与其父级类型相同的模板化静态常量成员
如果标题不清楚,代码应该有助于澄清:
// .h file
template<class T> class DF_Vector3
{
public:
T x, y, z;
static const DF_Vector3 ZERO;
DF_Vector3() {}
DF_Vector3( T F );
DF_Vector3( T X, T Y, T Z );
};
typedef DF_Vector3<DF_FLOAT> DF_Vector3F;
// .cpp file
template<> const DF_Vector3F DF_Vector3F::ZERO( 0.0f, 0.0f, 0.0f ); // ERROR: Explicit specialization of 'ZERO' after instantiation
编译器随后引用了引用 DF_Vector3F::ZERO 的另一个位置,并指出:“此处首先需要隐式实例化。”
Visual Studio 2010 编译器似乎并不介意这一点。然而,Apple 的 CLANG (LLVM) 编译器不喜欢它(它是抱怨的)。
有办法解决这个问题吗?谢谢。
If the title isn't clear, the code should help clarify:
// .h file
template<class T> class DF_Vector3
{
public:
T x, y, z;
static const DF_Vector3 ZERO;
DF_Vector3() {}
DF_Vector3( T F );
DF_Vector3( T X, T Y, T Z );
};
typedef DF_Vector3<DF_FLOAT> DF_Vector3F;
// .cpp file
template<> const DF_Vector3F DF_Vector3F::ZERO( 0.0f, 0.0f, 0.0f ); // ERROR: Explicit specialization of 'ZERO' after instantiation
The compiler subsequently cites another location where DF_Vector3F::ZERO is referenced and states: "Implicit instantiation first required here."
The Visual Studio 2010 compiler doesn't seem to mind this. However, Apple's CLANG (LLVM) compiler does not like it (it's the one complaining).
Is there a way to fix this? Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您不能在静态成员的定义中使用
typedef
,因为当您拥有typedef
时,类模板已经实例化,之后您无法显式地特化其成员。您应该这样做:
演示:http://ideone.com/AMWMC
You cannot use the
typedef
in the definition of the static members, because by the time you have atypedef
, the class template is already instantiated, after which you cannot explicity specialized its member(s).You should do this:
Demo : http://ideone.com/AMWMC