Namespaces
Variants
Actions

Move constructors

From cppreference.com
< cpp‎ | language
 
 
C++ language
General topics
Flow control
Conditional execution statements
if
Iteration statements (loops)
for
range-for (C++11)
Jump statements
Functions
Function declaration
Lambda function expression
inline specifier
Dynamic exception specifications (until C++17*)
noexcept specifier (C++11)
Exceptions
Namespaces
Types
Specifiers
const/volatile
decltype (C++11)
auto (C++11)
constexpr (C++11)
consteval (C++20)
constinit (C++20)
Storage duration specifiers
Initialization
Expressions
Alternative representations
Literals
Boolean - Integer - Floating-point
Character - String - nullptr (C++11)
User-defined (C++11)
Utilities
Attributes (C++11)
Types
typedef declaration
Type alias declaration (C++11)
Casts
Memory allocation
Classes
Class-specific function properties
explicit (C++11)
static

Special member functions
Default constructor
Copy constructor
Move constructor (C++11)
Templates
Miscellaneous
 
 

A move constructor is a constructor which can be called with an argument of the same class type and copies the content of the argument, possibly mutating the argument.

Contents

[edit] Syntax

class-name (parameter-list ); (1)
class-name (parameter-list ) function-body (2)
class-name (single-parameter-list ) = default; (3)
class-name (parameter-list ) = delete; (4)
class-name ::class-name (parameter-list ) function-body (5)
class-name ::class-name (single-parameter-list ) = default; (6)
class-name - the class whose move constructor is being declared
parameter-list - a non-empty parameter list satisfying all following conditions:
  • given the class type as T, the first parameter is of type T&&, const T&&, volatile T&& or const volatile T&&, and
  • either there are no other parameters, or all other parameters have default arguments
single-parameter-list - a parameter list of only one parameter, which is of type T&&, const T&&, volatile T&& or const volatile T&& and does not have a default argument
function-body - the function body of the move constructor

[edit] Explanation

1) Declaration of a move constructor inside of class definition.
2-4) Definition of a move constructor inside of class definition.
3) The move constructor is explicitly-defaulted.
4) The move constructor is deleted.
5,6) Definition of a move constructor outside of class definition (the class must contain a declaration (1)).
6) The move constructor is explicitly-defaulted.
struct X
{
    X(X&& other); // move constructor
//  X(X other);   // Error: incorrect parameter type
};
 
union Y
{
    Y(Y&& other, int num = 1); // move constructor with multiple parameters
//  Y(Y&& other, int num);     // Error: `num` has no default argument
};

The move constructor is typically called when an object is initialized (by direct-initialization or copy-initialization) from rvalue (xvalue or prvalue)(until C++17)xvalue(since C++17) of the same type, including

  • initialization: T a = std::move(b); or T a(std::move(b));, where b is of type T;
  • function argument passing: f(std::move(a));, where a is of type T and f is void f(T t);
  • function return: return a; inside a function such as T f(), where a is of type T which has a move constructor.

When the initializer is a prvalue, the move constructor call is often optimized out(until C++17)never made(since C++17), see copy elision.

Move constructors typically transfer the resources held by the argument (e.g. pointers to dynamically-allocated objects, file descriptors, TCP sockets, thread handles, etc.) rather than make copies of them, and leave the argument in some valid but otherwise indeterminate state. Since move constructor doesn’t change the lifetime of the argument, the destructor will typically be called on the argument at a later point. For example, moving from a std::string or from a std::vector may result in the argument being left empty. For some types, such as std::unique_ptr, the moved-from state is fully specified.

[