可选模板参数 [英] Optional Template parameter

查看:72
本文介绍了可选模板参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如在C ++中是否可以有可选的模板参数

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

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

我希望用户在V或没有V

正在关注

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

如果是,该怎么做.

推荐答案

您可以具有 default 模板参数,这些参数足以满足您的目的:

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

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

现在进行以下工作:

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

这篇关于可选模板参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆