W3cubDocs

/C++

Copy constructors

A copy constructor of class T is a non-template constructor whose first parameter is T&‍, const T&‍, volatile T&‍, or const volatile T&‍, and either there are no other parameters, or the rest of the parameters all have default values.

Syntax

class-name ( const class-name & ) (1)
class-name ( const class-name & ) = default; (2) (since C++11)
class-name ( const class-name & ) = delete; (3) (since C++11)

Where class-name must name the current class (or current instantiation of a class template), or, when declared at namespace scope or in a friend declaration, it must be a qualified class name.

Explanation

1) Typical declaration of a copy constructor.
2) Forcing a copy constructor to be generated by the compiler.
3) Avoiding implicit generation of the copy constructor.

The copy constructor is called whenever an object is initialized (by direct-initialization or copy-initialization) from another object of the same type (unless overload resolution selects a better match or the call is elided), which includes.

  • initialization: T a = b; or T a(b);, where b is of type T;
  • function argument passing: f(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 no move constructor.

Implicitly-declared copy constructor

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

  • each direct and virtual base B of T has a copy constructor whose parameters are const B& or const volatile B&;
  • each non-static data member M of T of class type or array of class type has a copy constructor whose parameters are const M& or const volatile M&.

Otherwise, the implicitly-declared copy constructor is T::T(T&). (Note that due to these rules, the implicitly-declared copy constructor cannot bind to a volatile lvalue argument.).

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

If some user-defined copy constructors are present, the user may still force the generation of the implicitly declared copy constructor with the keyword default.

(since C++11)

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

Deleted implicitly-declared copy constructor

The implicitly-declared copy constructor for class T is undefined if any of the following conditions are true:

(until C++11)

The implicitly-declared or defaulted copy constructor for class T is defined as deleted if any of the following conditions are true:

(since C++11)
  • T has non-static data members that cannot be copied (have deleted, inaccessible, or ambiguous copy constructors);
  • T has direct or virtual base class that cannot be copied (has deleted, inaccessible, or ambiguous copy constructors);
  • T has direct or virtual base class or a non-static data member with a deleted or inaccessible destructor;
  • T is a union-like class and has a variant member with non-trivial copy constructor;
  • T has a data member of rvalue reference type;
  • T has a user-defined move constructor or move assignment operator (this condition only causes the implicitly-declared, not the defaulted, copy constructor to be deleted).
(since C++11)

Trivial copy constructor

The copy constructor for class T 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 of T is trivial;
  • the copy constructor selected for every non-static class type (or array of class type) member of T 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. with std::memmove. All data types compatible with the C language (POD types) are trivially copyable.

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.

(since C++20)

Triviality of eligible copy constructors determines whether the class is an implicit-lifetime type, and whether the class is a trivially copyable type.

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 if odr-used or needed for constant evaluation (since C++11). For union types, the implicitly-defined copy constructor copies the object representation (as by std::memmove). For non-union class types (class and struct), the constructor performs full member-wise copy of the object's bases and non-static members, in their initialization order, using direct initialization.

If this satisfies the requirements of a constexpr constructor (until C++23)constexpr function (since C++23), the generated copy constructor is constexpr.

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

(since C++11)

Notes

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

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
}

Defect reports

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

DR Applied to Behavior as published Correct behavior
CWG 2094 C++11 volatile members make copy non-trivial (CWG issue 496) triviality not affected
CWG 2171 C++11 X(X&) = default was non-trivial made trivial

See also

© cppreference.com
Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.
https://en.cppreference.com/w/cpp/language/copy_constructor