类模板的静态成员错误

use*_*108 18 c++ language-lawyer c++11

我对此代码段有疑问:

template <typename T>
struct S
{
  static int a;
};

template <typename T>
decltype(S<T>::a) S<T>::a;
Run Code Online (Sandbox Code Playgroud)

clang-3.4 说:

s.cpp:8:25: error: redefinition of 'a' with a different type: 'decltype(S<T>::a)' vs 'int'
decltype(S<T>::a) S<T>::a;
                        ^
s.cpp:4:14: note: previous definition is here
  static int a;
             ^
1 error generated.
Run Code Online (Sandbox Code Playgroud)

gcc-4.8.2接受.哪个编译器是对的?我将来应该避免这样的代码吗?

Kla*_*aus 2

对我来说,clang 在这里被打破了。

所有与 decltype 的组合都会失败。没有 decltype 就可以工作。

template <typename T>
struct S
{   
      static int a;

      using type = decltype( a );
      typedef decltype( a ) type2;
};  

template <typename T>
1) decltype(S<T>::a) S<T>::a;
2) int S<T>::a;
3) typename S<T>::type S<T>::a;
4) typename S<T>::type2 S<T>::a;
Run Code Online (Sandbox Code Playgroud)

1 gcc 有效,clang 失败

2 gcc + clang 作品

3 gcc 有效,clang 失败

4 gcc 有效,clang 失败

我又做了一些尝试来解决这个问题,但没有取得任何成功。

关于此类问题还有一些更多讨论: C++ Static member initalization (template fun inside)

编辑:我发现这个主题到目前为止在标准中只是“未解决”,并且 clang 没有实现它:看看: http://clang.llvm.org/cxx_dr_status.html(点 205 )

我希望我没有误解该页面。请随意纠正我的解释。