Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
178 views
Welcome To Ask or Share your Answers For Others

1 Answer

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>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...