Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      Copy 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 copy constructor is aconstructor which can be called with an argument of the same class type and copies the content of the argument without 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)(since C++11)
      class-name (parameter-list ) = delete; (4)(since C++11)
      class-name ::class-name (parameter-list )function-body (5)
      class-name ::class-name (single-parameter-list ) = default; (6)(since C++11)
      class-name - the class whose copy 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 copy constructor

      [edit]Explanation

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

      The copy constructor is called whenever an object isinitialized (bydirect-initialization orcopy-initialization) from another object of the same type (unlessoverload resolution selects a better match or the call iselided), which includes

      • initialization:T a= b; orT a(b);, whereb is of typeT;
      • function argument passing:f(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 nomove constructor.

      [edit]Implicitly-declared copy constructor

      If no user-defined copy constructors are provided for a class type, the compiler will always declare a copy constructor as a non-explicitinlinepublic member of its class. This implicitly-declared copy constructor has the formT::T(const T&) if all of the following are true:

      • each direct and virtual baseB ofT has a copy constructor whose parameters are of typeconst B& orconstvolatile B&;
      • each non-static data memberM ofT of class type or array of class type has a copy constructor whose parameters are of typeconst M& orconstvolatile M&.

      Otherwise, the implicitly-declared copy constructor isT::T(T&).

      Due to these rules, the implicitly-declared copy constructor cannot bind to avolatile lvalue argument.

      A class can have multiple copy constructors, e.g. bothT::T(const T&) andT::T(T&).

      Even if some user-defined copy constructors are present, the user may still force the implicit copy constructor declaration with the keyworddefault.

      (since C++11)

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

      [edit]Implicitly-defined copy constructor

      If the implicitly-declared copy constructor is not deleted, it is defined (that is, a function body is generated and compiled) by the compiler ifodr-used orneeded for constant evaluation(since C++11). For union types, the implicitly-defined copy constructor copies the object representation (as bystd::memmove). For non-union class types, the constructor performs full member-wise copy of the object's direct base subobjects and member subobjects, in their initialization order, using direct initialization. For each non-static data member of a reference type, the copy 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 copy constructor isconstexpr.

      The generation of the implicitly-defined copy constructor is deprecated ifT has a user-defined destructor or user-defined copy assignment operator.

      (since C++11)

      [edit]Deleted copy constructor

      The implicitly-declared or explicitly-defaulted(since C++11) copy constructor for classT isundefined(until C++11)defined as deleted(since C++11) if any of the following conditions is satisfied:

      • T has a non-static data member of rvalue reference type.
      (since C++11)
      • M has a destructor that is deleted or(since C++11) inaccessible from the copy constructor, or
      • the overload resolution as applied to findM's copy constructor
      • does not result in a usable candidate, or
      • in the case of the subobject being avariant member, selects a non-trivial function.

      The implicitly-declared copy constructor for classT is defined as deleted ifT declares amove constructor ormove assignment operator.

      (since C++11)

      [edit]Trivial copy constructor

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

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

      A trivial copy constructor for a non-union class effectively copies every scalar subobject (including, recursively, subobject of subobjects and so forth) of the argument and performs no other action. However, padding bytes need not be copied, and even the object representations of the copied subobjects need not be the same as long as their values are identical.

      TriviallyCopyable objects can be copied by copying their object representations manually, e.g. withstd::memmove. All data types compatible with the C language (POD types) are trivially copyable.

      [edit]Eligible copy constructor

      A copy constructor is eligible if it is either user-declared or both implicitly-declared and definable.

      (until C++11)

      A copy constructor is eligible if it is not deleted.

      (since C++11)
      (until C++20)

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

      (since C++20)

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

      [edit]Notes

      In many situations, copy constructors are optimized out even if they would produce observable side-effects, seecopy elision.

      [edit]Example

      struct A{int n;    A(int n=1): n(n){}    A(const A& a): n(a.n){}// user-defined copy constructor}; struct B: A{// implicit default constructor B::B()// implicit copy constructor B::B(const B&)}; struct C: B{    C(): B(){}private:    C(const C&);// non-copyable, C++98 style}; int main(){    A a1(7);    A a2(a1);// calls the copy constructor     B b;    B b2= b;    A a3= b;// conversion to A& and copy constructor volatile A va(10);// A a4 = va; // compile error     C c;// C c2 = c; // compile error}

      [edit]Defect reports

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

      DRApplied toBehavior as publishedCorrect behavior
      CWG 1353C++98the conditions where implicitly-declared copy constructors
      are undefined did not consider multi-dimensional array types
      consider these types
      CWG 2094C++11volatile members make copy non-trivial (CWG issue 496)triviality not affected
      CWG 2171C++11X(X&)=default was non-trivialmade trivial
      CWG 2595C++20a copy constructor was not eligible if there is
      another copy 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/copy_constructor&oldid=172237"

      [8]ページ先頭

      ©2009-2025 Movatter.jp