在clang中实例化之后的静态成员初始化 [英] Static member initialization after instantiation in clang

查看:115
本文介绍了在clang中实例化之后的静态成员初始化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此类代码可由GCC编译,但clang 3.5失败。

Such code can be compiled by GCC, but clang 3.5 fails.

#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返回消息:

Clang returns message:

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编译器错误?

Is the error in code, or in clang compiler? Does it fulfill the standard, or GCC is more permissive and compiles non-standard code?

推荐答案

程序是否错误?它符合标准,还是GCC更宽松,并且可以编译非标准代码? C ++ 11 14.7.3 / 6说:

The program is wrong. C++11 14.7.3/6 says:


如果模板[...]是明确专用的,则应声明专用在第一次使用会导致隐式实例化
的专业化之前

If a template [...] is explicitly specialized then that specialization shall be declared before the first use of that specialization that would cause an implicit instantiation to take place

您的代码在 main ,在其中引起隐式实例化,然后在错误消息所描述的情况下稍后声明专门化。

Your code uses it in main, causing an implicit instantiation there, then declares the specialisation later, as the error message describes.

这篇关于在clang中实例化之后的静态成员初始化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆