Named? parameters in templates, functions
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 abl开发者_运维百科e to write the following:
having defined previously:
template<class T = int,class Policy_1, class Policy_2>
class X
{
};
then in main:
X<Policy_2: NoReturn> x;
this same with functions; having:
void f(int arg_1 = 0, int arg_2 = 1, int arg_3 = 2)
{
}
then in main:
f(arg_3: 55);
For functions you can use the Named Parameters Idiom (in both C++98 and C++0x).
See C++ FAQ item 10.20 What is the "Named Parameter Idiom"?.
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.
Cheers & hth.,
Not with that syntax and though it becomes easier to USE such constructs, creating them is rather messy.
See Boost.Parameter
You end up with
typedef template_by_named< policy1<type1>, policy2<type2> > x;
and
f(param_b = 23, param_d = 42)
This article describes a technique that can be used to emulate named template parameters.
Example syntax:
enum class bars { bar1, bar2, bar3 };
// Omitted definitions of get_value, is_present, get_type, a, b, c and d.
template <typename... Args>
struct foo {
static constexpr auto A = get_value<a<1>, Args...>::value;
static constexpr auto B = get_value<b<bars::bar2>, Args...>::value;
static constexpr auto C = is_present<c, Args...>::value;
using D = typename get_type<d<char>, Args...>::value;
};
// Client code
foo<d<float>, a<42>> f;
// f::A equals to 42;
// f::B equals to defaulted bars::bar2;
// f::C equals to false, because c is not present among temlate arguments;
// f::D equals to float
Nope, that's not going to work in C++0x.
The rest of us just used argument structs and got over it. And no.
精彩评论