12
votes

Consider the following small code fragment:

#include <iostream> 
    
template<class T> 
int test(); 
    
int main() 
{     
    std::cout << test<int>() << "\n"; 
} 

// 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?

2
From the quote you pasted: the standard does not make this clear, so I think you cannot have a valid answer unless you find a way to give the standard some clarity.mah
@TemplateRex The paragraph I quoted is also in C++11. It allows instantiating only at one POI and essentially ignoring the others. Combined with the C++11 "relaxation" of adding (allowing) another POI at the end of the TU, the behaviour of clang++/g++ should be allowed. (Btw, those issues have anchors, so you can append a #993 to the URL; wg21.cmeerw.net/cwg/issue993 is also possible)dyp
@TemplateRex where does Vandervoorde & Josuttis say that? That doesn't match my understanding. Remember the paragraph I quoted, which say that the function template only has to be defined somewhere in the TU, it doesn't have to preceed the point of instantiations. The point of instantiation modifies what the generated specialization is able to see (if you declared a function at the end of the TU, and used it in the template in a dependent manner, then you might use that fact to determine what POI clang uses).Johannes Schaub - litb
Conceptually even, the time where templates are instantiated is after all TUs are finished with processing, according to 2.2p8 ("Each translated translation unit is examined to produce a list of required instantiations. [...] The definitions of the required templates are located. [...] All the required instantiations are performed to produce instantiation units."). Implementations simplify this process (in accordance to as-if and footnote p11 "Implementations must behave as if these separate phases occur, although in practice different phases might be folded together.").Johannes Schaub - litb
@TemplateRex [temp.inst]/8 "If an implicit instantiation of a class template specialization is required and the template is declared but not defined, the program is ill-formed." There's no such rule for function templates as far as I know.dyp

2 Answers

8
votes

Core Working Group defect report 993 was created to address this issue:

993. Freedom to perform instantiation at the end of the translation unit

Section: 14.6.4.1 [temp.point] Status: C++11 Submitter: John Spicer Date: 6 March, 2009
[Voted into the WP at the March, 2011 meeting.]

The intent is that it is a permissible implementation technique to do template instantiation at the end of a translation unit rather than at an actual point of instantiation. This idea is not reflected in the current rules, however.

Proposed resolution (January, 2011):

Change 14.6.4.1 [temp.point] paragraph 7 as follows:

A specialization for a function template, a member function template, or of a member function or static data member of a class template may have multiple points of instantiations within a translation unit, and in addition to the points of instantiation described above, for any such specialization that has a point of instantiation within the translation unit, the end of the translation unit is also considered a point of instantiation. A specialization for a class template...

Paragraph 14.6.4.1/7 in C++11 is 14.6.4.1/8 in N3936:

A specialization for a function template, a member function template, or of a member function or static data member of a class template may have multiple points of instantiations within a translation unit, and in addition to the points of instantiation described above, for any such specialization that has a point of instantiation within the translation unit, the end of the translation unit is also considered a point of instantiation. A specialization for a class template has at most one point of instantiation within a translation unit. A specialization for any template may have points of instantiation in multiple translation units. If two different points of instantiation give a template specialization different meanings according to the one definition rule (3.2), the program is ill-formed, no diagnostic required.

So yes, it is allowable for implementations to delay the point of instantiation of templates to the end of the translation unit.

3
votes

Explicitly specializing after the explicit call to the template will fail at compilation.

#include <iostream> 

template<class T> 
int test(T y);


int main() 
{     
    std::cout << test<int>(0) << "\n"; 
}

template<class T> 
int test(T y) 
{ 
    return 0; 
}

// POI for test<int>() should be right here      

template<>
int test(int y) 
{ 
    return 2; 
}

Check the compilation error here

Compilation error    time: 0 memory: 0 signal:0
prog.cpp:21:15: error: specialization of ‘int test(T) [with T = int]’ after instantiation
int test(int y)