Not Your Father's C++

[article]

The parameter declaration identifies rhs to be an rvalue reference, so this function will only be called when assigning a temporary vector to the current object, as in

        v = get_vector();

The temporary vector returned by get_vector() will not be copied at all. Instead, its contents will be swapped (i.e., “moved”) into v, simply by interchanging the internal pointers in the respective vector objects. The old C++98 way using a const reference parameter requires allocating new memory, deleting the old memory, and copying the incoming contents into the current vector. All containers in C++11 have been rewritten to accommodate move semantics. Even if you never have to use rvalue references in your own code, you get a tremendous speedup by just using the new C++11 containers.

For an exhilarating look at C++11, peruse the proceedings of the May 2012 C++Now! Conference. For an excellent tutorial on rvalue references see David Abrahams online series.

About the author

Chuck Allison's picture Chuck Allison

Chuck Allison developed software for more than twenty years before becoming a professor of computer science at Utah Valley University. He is a technical editor for Better Software magazine and founding and current editor of the online journal, The C++ Source. He spent most of the 1990s as an active member of the C++ Standards Committee and is author of two C++ books, including Thinking In C++, Volume 2, with Bruce Eckel. His company, Fresh Sources, Inc., gives onsite training in C++, Python, and design patterns. His current top technical interest is the resurgence of functional programming. Whenever he finds a little down time he plays classical guitar or bikes the country roads of central Utah. Contact Chuck at chuck@freshsources.com.

AgileConnection is one of the growing communities of the TechWell network.

Featuring fresh, insightful stories, TechWell.com is the place to go for what is happening in software development and delivery.  Join the conversation now!

Upcoming Events

May 04
May 04
May 04
Jun 01