From the book - C++ Templates: The Complete Guide by David, Nicolai
Thus, templates are compiled twice:
- Without instantiation, the template code itself is checked for correct syntax. Syntax errors are discovered, such as missing semicolons.
- At the time of instantiation, the template code is checked to ensure that all calls are valid. Invalid calls are discovered, such as unsupported function calls.
Keeping the first point, I wrote -
template<typename T>
void foo( T x)
{
some illegal text
}
int main()
{
return 0;
}
It build fine on Visual Studio 2010 with out any warnings with optimizations turned off. How ever, it failed on gcc-4.3.4. Which one is complying to the C++ standard ? Is it mandatory for template code to get compiled even with out template instantiation ?
See Question&Answers more detail:os