2.2.1 The Move Constructor and Assignment Operator

In order to make it work, a new type of reference was added to the language; and classes can use this reference in constructors and assignment operators to implement the actual transfer of data from one instance to another. So to mark an argument as moveable, use &&:

struct foo 
{ 
  foo(foo&& that) 
    : m_resource(that.m_resource) 
  { 
    that.m_resource = nullptr; 
  } 
 
  foo& operator=(foo&& that) 
  { 
    std::swap(m_resource, that.m_resource); 
  } 
}; 
 
// All of these call the constructor defined above. 
foo f(foo()); 
foo f(construct_a_foo()); 
foo f(std::move(g)); 
 
// All of these call the assignment operator defined above. 
f = foo(); 
f = construct_a_foo(); 
f = std::move(g);

The parameter of such functions is considered a temporary on the call site. They are actually not restricted to constructors or assignment operators, and can be used in any function.

The semantics of such arguments must be interpreted as “I may steal your data”. The may is important here, as there is no guarantee on the call site that the instance going through an std::move is actually moved.