C++ template function default value

Try this:

template<class T> T sum(T a, T b, T c=T())
{
     return a + b + c;
}

You can also put in T(5) if you are expecting an integral type and want the default value to be 5.

Leave a Comment