14

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?

Community
  • 1
  • 1
TemplateRex
  • 69,038
  • 19
  • 164
  • 304
  • 1
    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 Apr 12 '14 at 12:43
  • @mah V&J thought (around 2000) that the then Standard wasn't clear. I wonder whether the language-lawyers here think the same about the current draft Standard quote. – TemplateRex Apr 12 '14 at 12:56
  • [temp.point] (C++03) mentions: "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 you can delay it until the end of the TU (see Casey's comment below), if that changes anything you don't have to diagnose that as far as I understand it. – dyp Apr 12 '14 at 13:07
  • 14.6.4.1/8: "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. ..." – Casey Apr 12 '14 at 13:07
  • @Casey make it an answer, and I'll accept. – TemplateRex Apr 12 '14 at 13:16
  • @dyp I found DR 993 http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html that was voted into the C++11 Standard as per Casey's quote. – TemplateRex Apr 12 '14 at 13:31
  • @TemplateRex I'm still trying to convince myself it *is* an answer – Casey Apr 12 '14 at 13:39
  • @Casey even if my code fragment is a single translation unit? – TemplateRex Apr 12 '14 at 13:40
  • 1
    @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; http://wg21.cmeerw.net/cwg/issue993 is also possible) – dyp Apr 12 '14 at 13:53
  • 1
    What's the point of your code? It doesn't matter whether the point of instantiation is at the end of the TU or after `main`, in that code. In both cases your code is well-formed, according to 14p6 (it suffices that there is a definition of the function template somewhere in the TU, or an explicit instantiation somewhere in the program). – Johannes Schaub - litb Apr 12 '14 at 14:48
  • @JohannesSchaub-litb sorry, which 14p6 are you referring to? – TemplateRex Apr 12 '14 at 16:54
  • 1
    @TemplateRex "A function template, [...] shall be defined in every translation unit in which it is implicitly instantiated unless the corresponding specialization is explicitly instantiated (14.7.2) in some translation unit" – Johannes Schaub - litb Apr 12 '14 at 17:06
  • @JohannesSchaub-litb but that only says *that* it has to be instantiated, not *at which point*. – TemplateRex Apr 12 '14 at 17:56
  • @TemplateRex I agree. But you seem to be saying that clang instantiates the template at the end of the TU because "that compiles and prints 0". That seems to be an incorrect conclusion. It could instantiate the template right after main and the program would still compile, as the only thing that the template does is `return 0;`, which works at both places. – Johannes Schaub - litb Apr 12 '14 at 18:03
  • @JohannesSchaub-litb my (mis?)understanding is that definitions have to be seen before a template can be instantiated (that was what I inferred from Vandevoorde & Josuttis anyway), so that if the POI is right after `main()`, then it would be an error. – TemplateRex Apr 12 '14 at 18:08
  • 1
    @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 Apr 12 '14 at 18:16
  • 1
    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 Apr 12 '14 at 18:18
  • @JohannesSchaub-litb in section 10.1 of V&J, after the first code example: "This explains why at point (6) in the source code, the class template definition must seen". – TemplateRex Apr 12 '14 at 18:29
  • @JohannesSchaub-litb and I realize that the V&J quote from 10.1 is about class templates, but it formed my understanding, together with the quote in the question (which is from V&J 10.3.2), that definitions have to be seen before their POI. – TemplateRex Apr 12 '14 at 18:43
  • 1
    @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 Apr 12 '14 at 19:34

2 Answers2

9

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.

Community
  • 1
  • 1
Casey
  • 41,449
  • 7
  • 95
  • 125
  • Credit to @TemplateRex for [finding the defect report](http://stackoverflow.com/questions/23030403/can-the-point-of-instantiation-be-delayed-until-the-end-of-the-translation-unit/23031288#comment35184814_23030403) that makes it clear that this change was introduced specifically to make the behavior observed in the question allowable. – Casey Apr 12 '14 at 14:00
  • tnx for writing it up. In hindsight, I feel slightly silly not to have searched for "end of translation unit" before posting here – TemplateRex Apr 12 '14 at 14:02
2

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) 
Casey
  • 41,449
  • 7
  • 95
  • 125
Vince
  • 3,497
  • 2
  • 19
  • 15
  • 3
    In template-speak, a *specialization* is the result of instantiating a template. Your program includes an *explicit specialization* of the `test` function template. – Casey Apr 12 '14 at 13:45