Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Static member initialization after instantiation in clang

Tags:

c++

gcc

clang

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 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;
                  ^

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

like image 339
nkdm Avatar asked Aug 08 '14 09:08

nkdm


1 Answers

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

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

like image 102
Mike Seymour Avatar answered Oct 12 '22 15:10

Mike Seymour