Channels ▼
RSS

C/C++

C++0x's Tools for Library Authors




Variadics

C++0x offers two different type-safe ways of dealing with variable numbers of arguments. One of them lets you give a collection of values of the same type as a single function argument; the other lets you give values of different types as separate arguments to template functions.


Giving a collection of values to a single function argument is what allows declarations such as



vector<int> v = { 3, 1, 4, 1, 5, 9 }; // New in C++0x, v has 6 elements

Under the hood, the standard-library vector code looks something like this:



template<class T> class vector {

    public:

           vector(initializer_list<T>);

     // …

};


The initializer_list template is built into the language. This constructor is used when we provide initializers inside curly braces. In the initialization of v, the compiler collects the values inside the braces, creates an object of type initializer_list<int>, and passes that object to the vector constructor. Within the constructor, the initializer_list object looks like a simplified container with begin and end members that vector can use to initialize its object with the values that the programmer supplied.

The other way to deal with variable numbers of arguments is with a variadic template:


             	template<class T...> void f(T... args);


This declaration effectively defines an overloaded family of functions:


 	void f();

     	template<class T1> void f(T1 arg1);

     	template<class T1, class T2> void f(T1 arg1, T2 arg2);

     	// and so on


The author of f can find out how many arguments f has, can forward those arguments to another function, and can process each argument independently, using overloading to check each argument's type during compilation. Unfortunately, the techniques for doing this need far more space than we have available at the moment; so we shall put off the detailed discussion for another day.

Conclusion

C++ has always tried to let programmers write libraries of useful algorithms, data structures, and interfaces. We have tried to give an idea of a few of the important ways in which C++0x continues this trend:

  • When we initialize a variable, the compiler can figure out its type for us.

  • We can extract type information from declarations and use it in other contexts.

  • We can initialize library types as conveniently as built-in types.

  • Smart pointer classes make memory allocation much easier.

  • Lambda expressions can substitute for simple functions or function objects.

  • Classes can arrange to move data without copying it.

  • Constructors are easier to write and offer more detailed control.

  • We can define functions with varying numbers of arguments and templates with varying numbers of type parameters.

This list barely scratches the surface. However, we believe that these features are among the most important, and that each of them will simplify C++ programming in a significant way.


Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.
 

Video