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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With