Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Named? parameters in templates, functions

Tags:

c++

c++11

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:

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);
like image 245
There is nothing we can do Avatar asked Nov 17 '10 21:11

There is nothing we can do


People also ask

What are template parameters?

In UML models, template parameters are formal parameters that once bound to actual values, called template arguments, make templates usable model elements. You can use template parameters to create general definitions of particular types of template.

What is the role of parameter in a template?

An identifier that names a non-type template parameter of class type T denotes a static storage duration object of type const T, called a template parameter object, whose value is that of the corresponding template argument after it has been converted to the type of the template parameter.

Can template have default parameters?

Template parameters may have default arguments. The set of default template arguments accumulates over all declarations of a given template.

Which template can have multiple parameters?

Explanation of the code: In the above program, the Test constructor has two arguments of generic type. The type of arguments is mentioned inside angle brackets < > while creating objects.


1 Answers

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.,

like image 137
Cheers and hth. - Alf Avatar answered Sep 21 '22 19:09

Cheers and hth. - Alf