2
votes
#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.

1
what is the error message?463035818_is_not_a_number
@idclev463035818 I have modified my question.xmh0511
You might want to change the explicit instantiation template void func<int>(int); into a specialization forward declaration template<> void func<int>(int); instead.Eljay

1 Answers

4
votes

It's a bit fragmented, but the relevant rule here is [temp.spec]/5

For a given template and a given set of template-arguments, [...]

  • both an explicit instantiation and a declaration of an explicit specialization shall not appear in a program unless the explicit instantiation follows a declaration of the explicit specialization. [...]