在哪里定义模板类的静态const成员变量 [英] Where to define static const member variables of a template class

查看:153
本文介绍了在哪里定义模板类的静态const成员变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据此问题

According to this question's answer:

根据标准,您必须定义i

...但是如果我为模板类的静态const成员变量本身是通用的,那么我得到链接错误(类似于如果模板代码在单独的编译单元中获取链接错误)。如果我在头中定义它们,链接错误消失了(我问是否可以在通用方式定义它们在此问题)。

... but if I do that for static const member variables of a template class which are themselves generic, then I get linking errors (similar to getting linking errors if the template code is in a separate compilation unit). If I define them in the header, the linking errors disappear (I asked whether it was OK to define them in a generic way in the first place in this question).

我在做什么,安全吗?下面是标题中的一个静态常量成员变量的定义。 p>

Is what I am doing, safe? Below is one of the definitions of a static const member variable which is in the header.

  template<typename T, unsigned int T_Size> 
  const Vector<T, T_Size> Vector<T, T_Size>::Zero = Vector<T, T_Size>(0);


推荐答案

必须定义类模板的静态数据成员在头文件中。只有在定义显式专用模板的静态成员时,才必须在实现文件中定义它们。

Static data members of a class template have to be defined in a header file. Only when you are defining static members of an explicitly specialized template, you have to define them in an implementation file.

换句话说,规则与类模板的成员函数相同。

In other words, the rule is the same as for member functions of class templates.

这篇关于在哪里定义模板类的静态const成员变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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