Optional Template parameter

Avinash picture Avinash · Oct 16, 2011 · Viewed 21.9k times · Source

Is it possible to have optional template parameter in C++ , for example

template < class T, class U, class V>
class Test {
};

Here I want user to use this class either with V or without V

Is following possible

Test<int,int,int> WithAllParameter
Test<int,int> WithOneMissing

If Yes how to do this.

Answer

ildjarn picture ildjarn · Oct 16, 2011

You can have default template arguments, which are sufficient for your purposes:

template<class T, class U = T, class V = U>
class Test
{ };

Now the following work:

Test<int> a;           // Test<int, int, int>
Test<double, float> b; // Test<double, float, float>