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

后端 未结 2 1027
醉话见心
醉话见心 2020-12-09 19:06

Consider the following small code fragment:

#include  
    
template 
int test(); 
    
int main() 
{     
    std::cout <&l         


        
相关标签:
2条回答
  • 2020-12-09 19:44

    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.

    0 讨论(0)
  • 2020-12-09 19:44

    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) 
    
    0 讨论(0)
提交回复
热议问题