Type condition in template

前端 未结 5 479
-上瘾入骨i
-上瘾入骨i 2020-12-05 06:27

Is it possible to build only some part of the code given the type of the template in C++ ? It would be something lake that :

#include 

using         


        
5条回答
  •  醉酒成梦
    2020-12-05 06:54

    Use template specialization:

    template
    void printType(T param)
    {
       // code for the general case - or omit the definition to allow only the specialized types
    }
    
    template<>
    void printType(char* param)
    {
       // code for char*
    }
    
    template<>
    void printType(int param)
    {
       // code for int    
    }
    
    // ...
    

提交回复
热议问题