Type condition in template

前端 未结 5 478
-上瘾入骨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:53

    You use template specification to specify versions of your function to work differently based on its type. For example, you can make a generic version of a function that would work with most types, and make a specific version for e.g. int that will be faster. You'd do it this way:

    template 
    void printType(T param)
    {
        cout<<"Generic version"<
    void printType(int param)
    {
        cout<<"Int version"<
    void printType(char param)
    {
        cout<<"Char version"<

提交回复
热议问题