What’s New in C++00X?

Get Ready for the New C++ Language Standards!

The next C++ standard, code-named “C++00X,” is heading down the home stretch towards ratification. The goal is to have the new standard completed by 2009. As with any International Standards Organization (ISO) standard, it requires worldwide cooperation among all the member nations to be official. The most recent ISO standard, C++98, now has several years of experience and results to build from. Actually, C++98 was updated in 2003. The “00X” part of the C++00X name recognizes that this is a multi-year process and the exact completion date could vary by a year or two. Currently, it is expected to be named C++09 as of the latest report (N2336). In this article, you won’t examine the ratification process itself but rather what C++00X means to the average C++ programmer in the trenches.

The Big Picture

To understand what is afoot with C++00X, you should first take a look at the current design goals of Bjarne Stroustrup, the man considered to be the father of modern C++. Stroustrup’s highest goals are to (1) increase the utility of C++ in developing operating system (OS) and library components and (2) to make C++ easier to learn and to teach. I first saw him at the Software Development ’99 conference in San Francisco and I recall the latter point was weighing heavily on his mind. Although there are lots of “opportunities” to increase the C++ footprint in the domains of numeric computation, windowing systems, and other commonly needed application environment components, Stroustrup feels strongly that these roles are better filled by existing open source or third-party suppliers.

Rolling in the TR1

The Technical Report 1 (TR1) library extensions were approved by the ISO committee in 2005 and are expected to be part of C++00X. Indeed, according to Scott Meyer’s site, Boost has already implemented functionality that is close or identical to most of the components of TR1. Gnu’s C++ compiler (GCC) Version 4 includes most of TR1 as of this writing. A quick overview of stuff from TR1 includes:

  • Reference Wrappers
  • Smart pointers (like Boost)
  • Function Objects
    • Polymorphic Function Wrapper: Stores all callables that use a specified function call signature
    • Function Object Binders: Binds parameters to function objects
    • Function Return Types: Determines the type of a call expression
    • mem_fn: Sllows pointers to member functions to be treated as function objects
  • Metaprogramming and Type Traits: Facilitates metaprogramming by enabling queries on and transformation between different types
  • Numerical Facilities
    • Random Number Generation
    • New Mathematical Functions
  • Containers
    • Tuple Types: Like an extension of “pair” (based on Boost)
    • Fixed Size Array (as opposed to vector)
    • Hash Tables: Unordered associative containers
  • Regular Expressions: Posix-like library
  • Better C Compatibility: Matches most C99 standards

New Language Syntax and Semantics

The latest and greatest information on C++00X as it stands was released in N2336 on July 29th, 2007. Several of the improvements are of interest only to programmers who develop C++ runtime libraries and compilers, but some of them have exciting possibilities for the average developer. In the remainder of this article, you’ll look at how some widely applicable proposed improvements to C++ could make your life better; these include multi-threading.

Delegating Constructors

Problem: In standard C++, constructors are not allowed to call other constructors. The syntax to do so simply doesn’t exist, so each constructor must construct all of its class members itself. This means you can end up with a lot of redundant (in other words, hard to maintain) initialization code or else create yet another function to do the common elements. Through delegation, C++0x will allow constructors to call other peer constructors. Other languages, such as Java, already provide this. For example,

class SomeType
{
   int number;

public:
   SomeType(int newNumber) : number(newNumber) {}
   SomeType() : SomeType(42) {}
};

In this example, the default constructor calls the peer constructor SomeType::SomeType(int) with the value 42. There’s more to consider with respect to the fact that C++00X considers the object “constructed” after the first constructor has been called. For details, see N1986.

Static Asserts

In today’s C++, neither the assert() macro nor #error can be used with templates. Because template instantiation doesn’t happen until after preprocessing, the #error is useless for templates. Similarly, the assert() macro is a runtime catcher and does not help catch issues that should be addressed at compile time. With C++0X, the static_assert allows simple and uniform compile time checking for a variety of scenarios. The usage is:

static_assert( constant-expression, error-message ) ;

You can use static_assert() in relatively simple checks:

static_assert( PI < 3.14 || PI > 3.15, " PI is inaccurate!" ) ;

Or, inside a template to do some type validations.

template< class T >
struct Check
{
   static_assert( sizeof(int) <= sizeof(T), "T is not big enough!" ) ;
} ;

For more info, consult N1720.

Template Aliasing

You can use template aliasing today only if you happen to have the complete parameter list defined by a typedef. Because you can’t create an alias with undefined parameters per se, a lot of code that uses STL looks harder to read (and write) than it really should. You can work around this issue today mostly by clever use of default parameters or by encapsulating it through yet another template. In C++00X, you will first declare the aliasing just ONCE with the “using” keyword:

template< class T > using Vector = MyVector< T, MyAlloc<T> > ;

Then, when it comes time to actually instantiate a variable, you can use the much easier to understand:

Vector<int> int_vector ;

Instead of the usual gobbledy-gook:

MyVector<int, MyAlloc<int> > int_vector ;

More by Author

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Must Read