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

后端 未结 2 1030
醉话见心
醉话见心 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

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

    #include  
    
    template 
    int test(T y);
    
    
    int main() 
    {     
        std::cout << test(0) << "\n"; 
    }
    
    template 
    int test(T y) 
    { 
        return 0; 
    }
    
    // POI for test() 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) 
    

提交回复
热议问题