Consider next example :
#include <iostream>
template< int a >
void foo();
int main(int argn, char* argv[])
{
foo<1>();
}
template<>
void foo<1>()
{
std::cout<<1<<std::endl;
}
The compilation fails with next error messages :
rg.cpp:12: error: specialization of ‘void foo() [with int a = 1]’ after instantiation
What paragraph in the standard explains this error?
PS :I know that if I move the function definition in front of main will make the error go away.
I think that's undefined behavior according to the standard. There are no restrictions on what a toolchain can do in cases of UB, generating a compiler error is one of the friendlier possibilities.
Section
[temp.spec]
, 14.7p5 saysSection
[temp.expl.spec]
14.7.3p6 says:Your program violates these requirements.