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 constructor
string two(one); // copy constructor – one is an lvalue
string 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 allows you to specify an allocator:
basic_string(const basic_string&& str, const Allocator&);
The following relationships hold after either of these two constructors is called:
• The data()
method returns a pointer to an allocated copy of the array whose first element is pointed to by str.data()
.
• The size()
method returns the value of str.size()
.
• The capacity()
method returns a value at least as large as size()
.
18.188.218.157