C++ template function default value

后端 未结 5 1960
挽巷
挽巷 2020-12-03 12:09

Is it possible to define the default value for variables of a template function in C++?

Something like below:

template T sum(T a, T b,         


        
5条回答
  •  鱼传尺愫
    2020-12-03 13:01

    Yes you can define a default value.

    template  
    T constructThird()
    {
        return T(1);
    }
    
    template  
    T test(T a, 
           T b, 
           T c = constructThird())
    {
        return a + b + c;
    }
    

    Unfortunately constructThird cannot take a and b as arguments.

提交回复
热议问题