【发布时间】:2017-11-10 20:02:45
【问题描述】:
示例代码在 Windows 和 Linux 上编译时不会出现警告或错误。它开始在 XCode 9 中收到 -Wundefined-var-template 警告。
foo.h:
template <typename T>
struct myClass
{
static const char* name;
};
foo.cpp:
#include "foo.h"
template<>
const char *myClass<int>::name = "int";
warning: instantiation of variable 'myClass<int>::name' required here, but no definition is available [-Wundefined-var-template]
note: forward declaration of template entity is here
static const char *name;
^
note: add an explicit instantiation declaration to suppress this warning if 'myClass<int>::name' is explicitly instantiated in another translation unit
【问题讨论】:
-
不是重复的,因为静态变量不能内联,所以不能像函数一样处理。请参阅我自己的答案中的“我尝试过但没用的”部分。
-
当他们是模板成员时可以。这是一个更好的骗局:stackoverflow.com/questions/3229883/…
-
...hm 实际上我现在不确定了,因为
myClass<int>是一个具体的类型,而不是模板了。 -
感谢您的调查,tobi303。我尝试了解决方案stackoverflow.com/questions/3229883/…,如“我尝试过但没有用”中的第二个项目符号中所述。