我有一个模板类,我只打算用3种不同类型,我提前知道的时候使用。为了减少代码膨胀我想保持尽可能的头外面。模板类也有静态变量必须依赖于专业化分工是不同的。
我试图完成这两个在Windows上使用Visual C ++ 19.15.26729和Mac用Xcode和铛 - 900.0.39.2。我需要不同的代码,以满足各编译器的,更糟的是编译器抱怨程序的对方的“好”的版本。
下面是一个小例子:
// A.h
template<typename T>
class A
{
public:
static T x;
};
// template<> int A<int>::x; // PROBLEMATIC PART
extern template class A<int>;
// A.cpp
#include "A.h"
template<> int A<int>::x = 42;
template class A<int>;
// main.cpp
#include "A.h"
int main()
{
return A<int>::x;
}
上述(与线注释)代码编译VC ++的很好,但铛抱怨:Explicit specialization of 'x' after instantiation
回答这个问题的帮助:What's the right way to specialize a template when using "extern template"?
随着template<> int A<int>::x;
注释去掉,它编译在Xcode的罚款,但随后的Visual C ++抱怨:
1>A.cpp(3): error C2086: 'T A<int>::x': redefinition
1> with
1> [
1> T=int
1> ]
1>A.h(9): note: see declaration of 'x'
1>A.cpp(3): error C2086: 'T A<T>::x': redefinition
1> with
1> [
1> T=int
1> ]
1>A.h(6): note: see declaration of 'A<int>::x'
是我的方法从根本上错了吗?它是一个编译器错误?也许这仅仅是通过编译器的一个支持的特性,如果是的话 - 根据标准哪个版本是正确的?
MSVC是错在这里了:“有问题”的行不是定义,因为它has no initializer。
锵是同时正确拒绝版本,而该声明是因为显式实例声明also an explicit instantiation declaration of A<int>::x
。
这的确是在微软编译器的一个bug,已经登录了他们的积压。按照此问题的更新:https://developercommunity.visualstudio.com/content/problem/319447/explicit-specialization-of-static-data-member-inco.html