模板类静态成员变量在使用'extern template class'语法时的特殊化
Specialization of a template class static member variable when also using 'extern template class' syntax
我有一个模板类,我只打算将它与我提前知道的3种不同类型一起使用。为了减少代码膨胀,我希望尽可能多地保留在标头之外。模板类还具有静态变量,这些变量必须根据专业化而有所不同。
我尝试在使用Visual C++19.15.26729的Windows和使用XCode和clang-900.0.39.2的Mac上实现这一点。我需要不同的代码来满足每个编译器,更糟糕的是,编译器抱怨对方的程序版本"好"。
这里有一个最小的例子:
// 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++上编译得很好,但clang抱怨道:Explicit specialization of 'x' after instantiation
这个问题的答案很有帮助:What';当使用";外部模板";?
在未注释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'
我的做法根本错误吗?这是编译器错误吗?也许这是一个只有一个编译器支持的功能,如果是这样的话——根据标准,哪个版本是正确的?
这确实是Microsoft编译器中的一个错误,已经记录在他们的积压工作中。关注此问题以获取更新:https://developercommunity.visualstudio.com/content/problem/319447/explicit-specialization-of-static-data-member-inco.html
MSVC在这里是错误的:"有问题的"行不是定义,因为它没有初始值设定项。
同时,Clang拒绝没有该声明的版本是正确的,因为显式实例化声明也是A<int>::x
的显式实例化宣言。
- C++ class template
- template<class T, int N> void h(T const(&)[N]); as friend function
- "template <class _Tp, class _Alloc = __STL_DEFAULT_ALLOCATOR(_Tp) >"=是什么意思?
- 有没有办法在Python中做"template base class"?
- 'inherit class'中类型 'template class' 引用的初始化无效
- "template<class C> void mini(C &a4, C b4) { a4 = min(a4, b4); }"定义的含义是什么?
- SFINAE 优雅地检查"template template class"(在模板参数中)
- 如何修复错误消息,如"argument list for class template is missing"?
- C++ "argument list for class template "迭代器" is missing"错误
- 定义模板类,但获取'is not a class template'
- C++ - shared_ptr "Unspecialised class template"错误
- "unspecialized class template can't be used as a template argument"是什么意思?
- class template, allocator_traits
- MSVC:"use of class template requires template argument list" STL 容器内
- class template cpp, hpp, cpp
- "use of class template requires template argument list"错误,即使使用了模板的模板,因此模板类是预期的
- "class template has already been declared as a non-class template"
- "use of class template requires template argument list"是什么意思?
- 模板模板部分专用化失败:"expected a class template"
- 使"class template"成为类好友的正确语法