Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can the point-of-instantiation be delayed until the end of the translation unit?

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?

like image 399
TemplateRex Avatar asked Apr 12 '14 12:04

TemplateRex


1 Answers

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.

like image 70
Casey Avatar answered Oct 23 '22 02:10

Casey