这样的代码可以用 GCC 编译,但 clang 3.5 失败。
#include <iostream>
using namespace std;
template<typename T>
class C{
public:
const static int x;
};
int main(){
cout << C<int>::x;
}
template<>
const int C<int>::x = 4;
Clang 返回消息:
hello.cpp:15:19: error: explicit specialization of 'x' after instantiation
const int C<int>::x = 4;
^
hello.cpp:11:19: note: implicit instantiation first required here
cout << C<int>::x;
^
错误是在代码中还是在 clang 编译器中?它是否满足标准,或者GCC更宽容并编译非标准代码?
程序错误。 C++11 14.7.3/6 说:
如果模板 [...] 是显式专门化的,则应在第一次使用该专门化之前声明该专门化,这将导致隐式实例化
发生
您的代码将其用于main
,导致在那里进行隐式实例化,然后稍后声明专业化,如错误消息所述。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)