C/C++

C++0x's Tools for Library Authors

By Andrew Koenig and Barbara E. Moo, August 29, 2011

Four additions to the language greatly facilitate writing C++ libraries.

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

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:

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.

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.

Video

This month's Dr. Dobb's Journal

This month,
Dr. Dobb's Journal is devoted to mobile programming. We introduce you to Apple's new Swift programming language, discuss the perils of being the third-most-popular mobile platform, revisit SQLite on Android
, and much more!