Constructors That Use an Rvalue Reference (C++11)

C++11 adds move semantics to the string class. As described in Chapter 18, this involves adding a move constructor, which uses an rvalue reference instead of an lvalue reference:

basic_string(basic_string&& str) noexcept;

This constructor is invoked when the actual argument is a temporary object:

string one("din");     // C-style string constructorstring two(one);       // copy constructor – one is an lvaluestring three(one+two); // move constructor, sum is an rvalue

As discussed in Chapter 18, the intent is that string three takes ownership of the object constructed by operator+() rather than copying the object and then letting the original be destroyed.

The second rvalue constructor additionally ...

Get C++ Primer Plus now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.