While experimenting with the recent g++-5 compiler, I wrote below statement in a file:
template<T> T a;
template<> int a = 1;
Which results in:
warning: too many template headers for
a
(should be 0)
Also effectively, it doesn't really specialize a<int>
. e.g.
template<typename T> T a;
template<> int a = 1;
int main () {
std::cout << a<double> << "\n"; // prints 0; OK
std::cout << a<int> << "\n"; // prints 0! why not 1?
}
What is the mystery about this syntax?
int a = 1;
should have 0 template headers, wheretemplate<
whatever>
is one template header. Imagine if you deleted thetemplate<class T> T a;
before it -- the warning makes sense. Still shocking that it is a warning, not an error. – Yakk - Adam Nevraumonttemplate<typename T> T a = 1; template<> int a = 0;
, if you put this statement in a common header file then the ... statement will result in linker error. – PaperBirdMaster