Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 同时使用';外部模板类';语法_C++_Templates_Visual C++_Clang_Template Specialization - Fatal编程技术网

C++ 同时使用';外部模板类';语法

C++ 同时使用';外部模板类';语法,c++,templates,visual-c++,clang,template-specialization,C++,Templates,Visual C++,Clang,Template Specialization,我有一个模板类,我只打算使用3种不同的类型,我知道提前。为了减少代码膨胀,我希望尽可能多地保留在标题之外。模板类还具有静态变量,这些变量必须根据专门化的不同而有所不同 我尝试用Visual C++ +19.1526729和MAC和XCODEL和CLAN-900.0.37.2实现Windows上的这两种功能。我需要不同的代码来满足每一个编译器,更糟糕的是,编译器抱怨彼此的“好”版本的程序 下面是一个简单的例子: // A.h template<typename T> class A {

我有一个模板类,我只打算使用3种不同的类型,我知道提前。为了减少代码膨胀,我希望尽可能多地保留在标题之外。模板类还具有静态变量,这些变量必须根据专门化的不同而有所不同

我尝试用Visual C++ +19.1526729和MAC和XCODEL和CLAN-900.0.37.2实现Windows上的这两种功能。我需要不同的代码来满足每一个编译器,更糟糕的是,编译器抱怨彼此的“好”版本的程序

下面是一个简单的例子:

// A.h
template<typename T>
class A
{
public:
    static T x;
};

// template<> int A<int>::x; // PROBLEMATIC PART

extern template class A<int>;

我的方法根本错误吗?这是一个编译器错误吗?可能这是一个仅由一个编译器支持的功能,如果是的话-根据标准,哪个版本是正确的?

MSVC在这里是错误的:“有问题”行不是一个定义,因为它是正确的


同时,Clang拒绝没有该声明的版本是正确的,因为显式实例化声明是正确的。

这确实是Microsoft编译器中的一个bug,已经记录在他们的backlog中。按照此问题进行更新:

您是否尝试过不使用
模板class A?我现在试过了,变化不大,不适用于每个编译器的版本仍然不起作用。那些版本确实如此,现在仍然如此。谢谢你的回答。到目前为止,我的解决方案是使用特定于编译器的宏来解决这个问题。这个错误已经在VisualStudio2019版本16.5预览2中修复。
// 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;
}
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'