Languages - DevSource
DevSource: Microsoft Developer Resource DevSource Home Sponsored by Microsoft Home Add Ons Architecture Languages Techniques Using VS Forums
Home arrow Languages arrow Page 2 - C++09: A Glimpse into the Future
C++09: A Glimpse into the Future
By Danny Kalev

Rate This Article: Add This Article To:

C++09: A Glimpse into the Future - ' Rvalue References '
( Page 2 of 3 )

Following the Portland meeting in October 2006, the C++ standardization committee set 2009 as the target date for C++09, the next C++ standard. C++09 will include at least two major core features: rvalue references and type concepts. Other core features are also included, such as automatic deduction of types from initializers, delegating constructors, nullptr, and even a solution to the right angle bracket nuisance. Join me for an overview of core C++09.

C++: the State of the Art

ADVERTISEMENT

Today's C++ already provides significant enhancement to C++98. These include subtle technical corrigenda such as vector's contiguity. However, the most important change between C++98 and C++03 is the addition of TR1 libraries which include hashed containers, regular expressions, tuples, new smart pointer classes, and an array class template.

A detailed description of TR1 libraries is available here.

TR1-enabled implementations of the Standard Library are already available. TR1 was only the aperitif, though. Core C++ is heading for no less than a revolution.

Rvalue References

C++09 introduces a new category of reference types called rvalue references. Consequently, the traditional reference types that you've been using for decades are now called lvalue references. Unlike lvalue references, rvalue references can bind to rvalues even if they are not const qualified. Rvalue references enable programmers to:

  • Eliminate unnecessary and expensive copies of objects by means of implementing move semantics
  • Permit users to implement perfect forwarding, thereby solve major usability problems with generic libraries
  • Solve usability problems in components where binding an rvalue to a non-const reference is not a logical error

Before discussing the importance of move semantics, let's look at the syntax of rvalue references. Whereas T& denotes an lvalue reference to T, T&& is an rvalue reference to T. Consider:

void g(int & n);
void h(int&& n);
g(5); //error, 5 is an rvalue
g(int()); //error, temporaries are rvalues
h(5); //OK
h(int()); //OK
S s;
S& rs=s; //ordindary lvalue reference
S&& rrs=S(); //OK, bind a temporary to an rvalue reference
S& illegal = S();//error, bind a temp to an lvalue reference

The Move Movement

C++ has been a copy-semantics language since its inception. Elementary language constructs such as copy constructors, assignment expressions, and argument passing are based on copying an existing object into a new target object. This notion is so fundamental in C++ that it takes time to grasp how significantly different move semantics is. Put simply, moving means constructing the target object directly into the source object. Consequently, you end up having a single object with a new state rather than having two objects that are copies of each other.

C++98 already implements move semantics in several places. The Named Return Value Optimization is a manifestation of the move semantics.

The swap() member function of STL containers also implements move semantics. When you swap two vectors using vector::swap(), the two containers don't copy-construct their elements in new memory destinations; they merely exchange a few pointers and flags. The performance gain can be dramatic. Copying a container of N elements is a linear operation that involves N copy constructor calls + N destructor calls. By contrast, moving is a fast constant time operation.

Another instance of move semantics is the std::auto_ptr class. It has been considered the step child of the Standard Library due to its "peculiar" copy semantics (which is actually pure move semantics!). However, with the advent of rvalue references, it is likely that move-based algorithms and containers will re-enliven the interest in classes such as auto_ptr.



 
 
>>> More Languages Articles          >>> More By Danny Kalev
 



Microsoft's Future: A Chat With Their CTO, Barry Briggs

Play Video >

All Videos >

Julia explores the Robotics Studio!

Read now >

Messages to Bill Gates!

Read now >

View Now
DevSource RSS FEEDS
XML Want an easy way to keep up with breaking tech news? And the Get DevSource headlines delivered to your desktop with RSS.