Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      Move constructors

      From cppreference.com
      <cpp‎ |language
       
       
      C++ language
      General topics
      Flow control
      Conditional execution statements
      Iteration statements (loops)
      Jump statements
      Functions
      Function declaration
      Lambda function expression
      inline specifier
      Dynamic exception specifications(until C++17*)
      noexcept specifier(C++11)
      Exceptions
      Namespaces
      Types
      Specifiers
      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
      Special member functions
      Templates
      Miscellaneous
       
       

      A move constructor is aconstructor 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-emptyparameter list satisfying all following conditions:
      • given the class type asT, the first parameter is of typeT&&,const T&&,volatile T&& orconstvolatile T&&, and
      • either there are no other parameters, or all other parameters havedefault arguments
      single-parameter-list - aparameter list of only one parameter, which is of typeT&&,const T&&,volatile T&& orconstvolatile T&& and does not have a default argument
      function-body - thefunction 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 isinitialized (bydirect-initialization orcopy-initialization) fromrvalue (xvalue or prvalue)(until C++17)xvalue(since C++17) of the same type, including

      • initialization:T a= std::move(b); orT a(std::move(b));, whereb is of typeT;
      • function argument passing:f(std::move(a));, wherea is of typeT andf isvoid f(T t);
      • function return:return a; inside a function such asT f(), wherea is of typeT which has a move constructor.

      When the initializer is a prvalue, the move constructor call isoften optimized out(until C++17)never made(since C++17), seecopy 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 astd::string or from astd::vector may result in the argument being left empty. For some types, such asstd::unique_ptr, the moved-from state is fully specified.

      [edit]Implicitly-declared move constructor

      If no user-defined move constructors are provided for a class type, and all of the following is true:

      Then the compiler will declare a move constructor as a non-explicitinlinepublic member of its class with the signatureT::T(T&&).

      A class can have multiple move constructors, e.g. bothT::T(const T&&) andT::T(T&&). If some user-defined move constructors are present, the user may still force the generation of the implicitly declared move constructor with the keyworddefault.

      The implicitly-declared (or defaulted on its first declaration) move constructor has an exception specification as described indynamic exception specification(until C++17)noexcept specification(since C++17).

      [edit]Implicitly-defined move constructor

      If the implicitly-declared move constructor is neither deleted nor trivial, it is defined (that is, a function body is generated and compiled) by the compiler ifODR-use orneeded for constant evaluation. For union types, the implicitly-defined move constructor copies the object representation (as bystd::memmove). For non-union class types, the move constructor performs full member-wise move of the object's direct base subobjects and member subobjects, in their initialization order, using direct initialization with anxvalue argument. For each non-static data member of a reference type, the move constructor binds the reference to the same object or function to which the source reference is bound.

      If this satisfies the requirements of aconstexpr constructor(until C++23)constexpr function(since C++23), the generated move constructor isconstexpr.

      [edit]Deleted move constructor

      The implicitly-declared or explicitly-defaulted move constructor for classT is defined as deleted ifT has apotentially constructed subobject of class typeM (or possibly multi-dimensional array thereof) such that

      • M has a destructor that is deleted or inaccessible from the copy constructor, or
      • the overload resolution as applied to findM's move constructor
      • does not result in a usable candidate, or
      • in the case of the subobject being avariant member, selects a non-trivial function.

      Such a constructor is ignored byoverload resolution (otherwise it would prevent copy-initialization from rvalue).

      [edit]Trivial move constructor

      The move constructor for classT is trivial if all of the following is true:

      • it is not user-provided (meaning, it is implicitly-defined or defaulted);
      • T has no virtual member functions;
      • T has no virtual base classes;
      • the move constructor selected for every direct base ofT is trivial;
      • the move constructor selected for every non-static class type (or array of class type) member ofT is trivial.

      A trivial move constructor is a constructor that performs the same action as the trivial copy constructor, that is, makes a copy of the object representation as if bystd::memmove. All data types compatible with the C language are trivially movable.

      [edit]Eligible move constructor

      A move constructor is eligible if it is not deleted.

      (until C++20)

      A move constructor is eligible if all following conditions are satisfied:

      (since C++20)

      Triviality of eligible move constructors determines whether the class is animplicit-lifetime type, and whether the class is atrivially copyable type.

      [edit]Notes

      To make thestrong exception guarantee possible, user-defined move constructors should not throw exceptions. For example,std::vector relies onstd::move_if_noexcept to choose between move and copy when the elements need to be relocated.

      If both copy and move constructors are provided and no other constructors are viable, overload resolution selects the move constructor if the argument is anrvalue of the same type (anxvalue such as the result ofstd::move or aprvalue such as a nameless temporary(until C++17)), and selects the copy constructor if the argument is anlvalue (named object or a function/operator returning lvalue reference). If only the copy constructor is provided, all argument categories select it (as long as it takes a reference to const, since rvalues can bind to const references), which makes copying the fallback for moving, when moving is unavailable.

      [edit]Example

      Run this code
      #include <iomanip>#include <iostream>#include <string>#include <utility> struct A{std::string s;int k;     A(): s("test"), k(-1){}    A(const A& o): s(o.s), k(o.k){std::cout<<"move failed!\n";}    A(A&& o)noexcept:        s(std::move(o.s)),// explicit move of a member of class type        k(std::exchange(o.k,0))// explicit move of a member of non-class type{}}; A f(A a){return a;} struct B: A{std::string s2;int n;// implicit move constructor B::(B&&)// calls A's move constructor// calls s2's move constructor// and makes a bitwise copy of n}; struct C: B{    ~C(){}// destructor prevents implicit move constructor C::(C&&)}; struct D: B{    D(){}    ~D(){}// destructor would prevent implicit move constructor D::(D&&)    D(D&&)=default;// forces a move constructor anyway}; int main(){std::cout<<"Trying to move A\n";    A a1= f(A());// return by value move-constructs the target// from the function parameter std::cout<<"Before move, a1.s = "<<std::quoted(a1.s)<<" a1.k = "<< a1.k<<'\n';     A a2= std::move(a1);// move-constructs from xvaluestd::cout<<"After move, a1.s = "<<std::quoted(a1.s)<<" a1.k = "<< a1.k<<'\n';  std::cout<<"\nTrying to move B\n";    B b1; std::cout<<"Before move, b1.s = "<<std::quoted(b1.s)<<"\n";     B b2= std::move(b1);// calls implicit move constructorstd::cout<<"After move, b1.s = "<<std::quoted(b1.s)<<"\n";  std::cout<<"\nTrying to move C\n";    C c1;    C c2= std::move(c1);// calls copy constructor std::cout<<"\nTrying to move D\n";    D d1;    D d2= std::move(d1);}

      Output:

      Trying to move ABefore move, a1.s = "test" a1.k = -1After move, a1.s = "" a1.k = 0 Trying to move BBefore move, b1.s = "test"After move, b1.s = "" Trying to move Cmove failed! Trying to move D

      [edit]Defect reports

      The following behavior-changing defect reports were applied retroactively to previously published C++ standards.

      DRApplied toBehavior as publishedCorrect behavior
      CWG 1353C++11the conditions where defaulted move constructors are
      defined as deleted did not consider multi-dimensional array types
      consider these types
      CWG 1402C++11a defaulted move constructor that would call
      a non-trivial copy constructor was defined as
      deleted; a defaulted move constructor that is
      deleted still participated in overload resolution
      allows call to such copy
      constructor; made ignored
      in overload resolution
      CWG 1491C++11a defaulted move constructor of a class with a non-static data
      member of rvalue reference type was defined as deleted
      not deleted in this case
      CWG 2094C++11a volatile subobject made a defaulted
      move constructor non-trivial (CWG issue 496)
      triviality not affected
      CWG 2595C++20a move constructor was not eligible if there is
      another move constructor which is more constrained
      but does not satisfy its associated constraints
      it can be eligible in this case

      [edit]See also

      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/language/move_constructor&oldid=176536"

      [8]ページ先頭

      ©2009-2025 Movatter.jp