Consider the following small code fragment:
#include <iostream>
template<class T>
int test();
int main()
{
std::cout << test<int>() << "
";
}
// POI for test<int>() should be right here
template<class T>
int test()
{
return 0;
}
Live Example that compiles and prints 0 for both Clang and g++.
Here's the draft Standard quote on the point of instantiation of function templates
14.6.4.1 Point of instantiation [temp.point]
1 For a function template specialization, a member function template specialization, or a specialization for a
member function or static data member of a class template, if the specialization is implicitly instantiated
because it is referenced from within another template specialization and the context from which it is referenced
depends on a template parameter, the point of instantiation of the specialization is the point of
instantiation of the enclosing specialization. Otherwise, the point of instantiation for such a specialization
immediately follows the namespace scope declaration or definition that refers to the specialization.
Vandevoorde and Josuttis have the following to say about this:
In practice, most compilers delay the actual instantiation of
noninline function templates to the end of the translation unit. This
effectively moves the POIs of the corresponding template
specializations to the end of the translation unit. The intention of
the C++ language designers was for this to be a valid implementation
technique, but the standard does not make this clear.
Question: are Clang/g++ non-conforming because they delay the POI to the end of the translation unit?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…