叫什么名字模板,功能中的参数 [英] Named? parameters in templates, functions

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

问题描述

即将发布的C ++ 0x标准中是否对模板和/或函数中的命名参数进行了任何更新?例如,我希望能够编写以下内容:

Is there any update in the upcoming C++0x standard on named parameters in templates and/or functions? For example, I would like to be able to write the following:

先前已定义:

template<class T = int,class Policy_1, class Policy_2>
class X
{
};

然后在主目录中:

X<Policy_2: NoReturn> x;

与功能相同;

void f(int arg_1 = 0, int arg_2 = 1, int arg_3 = 2)
{
}

然后在主目录中:

f(arg_3: 55);


推荐答案

对于函数,可以使用命名参数习惯用语(在C ++ 98和C ++ 0x中都使用)。

For functions you can use the Named Parameters Idiom (in both C++98 and C++0x).

请参阅C ++常见问题解答 10.20什么是命名参数成语?

See C++ FAQ item 10.20 What is the "Named Parameter Idiom"?.

对于模板参数,我认为您可以使用包装的想法,即使用类型载体类型,这些类型通过它们的类型编码它们是哪个模板参数。它变得复杂。您可能会在Boost参数库中找到想法,但从本质上讲,对于模板参数,我认为不值得花时间(更不用说实际使用)了-这是学术性的。

For template arguments I think you can use the idea of wrapping, using "type carrier" types that by their type encode which template argument they are. It gets complex. You might check out the Boost Parameters library for ideas, but essentially, for template arguments I do not think it's worth spending time on (not to mention actually using) -- it's academic.

干杯hth。,

这篇关于叫什么名字模板,功能中的参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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