#include <iostream>
template<typename T>
void func(T){}
template void func<int>(int);
template<>
void func<int>(int){
}
int main(){
}
Consider the above code, Clang
and GCC
both complain such code is ill-formed, as the below outcome noted.
explicit specialization of 'func<int>' after instantiation
However, I only find the similar rule :
temp.expl.spec#6
If a template, a member template or a member of a class 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, in every translation unit in which such a use occurs; no diagnostic is required. If the program does not provide a definition for an explicit specialization and either the specialization is used in a way that would cause an implicit instantiation to take place or the member is a virtual member function, the program is ill-formed, no diagnostic required. An implicit instantiation is never generated for an explicit specialization that is declared but not defined.
I think such code does not violate the above rule, Note the emphasized part, it says implicit instantiation
, In my example, such declaration template void func<int>(int);
is a explicit instantiation definition rather than the specialization that would case an implicit instantiation, So why the above code is ill-formed? what's the rule in the standard does the above code violate? Please point the rule out. Thanks.
template void func<int>(int);
into a specialization forward declarationtemplate<> void func<int>(int);
instead. – Eljay