Copy constructors

< cpp‎ | language
Revision as of 21:38, 2 November 2012 by P12bot (Talk | contribs)

C++ language
General topics
Flow control
Conditional execution statements
Iteration statements (loops)
Jump statements
Function declaration
Lambda function declaration
inline specifier
Exception specifications (deprecated)
noexcept specifier (C++11)
decltype (C++11)
auto (C++11)
alignas (C++11)
Storage duration specifiers
Alternative representations
Boolean - Integer - Floating-point
Character - String - nullptr (C++11)
User-defined (C++11)
Attributes (C++11)
typedef declaration
Type alias declaration (C++11)
Implicit conversions - Explicit conversions
static_cast - dynamic_cast
const_cast - reinterpret_cast
Memory allocation
Class-specific function properties
Special member functions
Default constructor
Copy constructor
Move constructor (C++11)

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. A type with a public copy constructor is CopyConstructible.



Template:sparam ( const Template:sparam & ) (1)
Template:sparam ( const Template:sparam & ) = default; (1)
Template:sparam ( const Template:sparam & ) = delete; (1)


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

The copy constructor is called whenever an object is initialized from another object of the same type, 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 an inline public member of its class. This implicitly-declared copy constructor has the form T::T(const T&) if all of the following is true:

  • all direct and virtual bases of T have copy constructors with references to const or to const volatile as their first parameters
  • all non-static members of T have copy constructors with references to const or to const volatile as their first parameters

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).

Deleted implicitly-declared copy constructor

The implicitly-declared or defaulted copy constructor for class T is undefined (until C++11) / defined as deleted (since C++11) in any of the following is true:

  • 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 with a deleted or inaccessible destructor
  • T has a user-defined move constructor or move assignment operator (since C++11)
  • T is a union and has a variant member with non-trivial copy constructor (since C++11)
  • T has a data member of rvalue reference type (since C++11)

Trivial copy constructor

The implicitly-declared copy constructor for class T is trivial if all of the following is true:

  • 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) memeber of T is trivial

A trivial copy constructor is a constructor that creates a bytewise copy of the object representation of the argument, and performs no other action. Objects with trivial copy constructors 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.

Implicitly-defined copy constructor

If the implicitly-declared copy constructor is not deleted or trivial, it is defined (that is, a function body is generated and compiled) by the compiler. 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.

The generation of the implicitly-defined copy constructor is deprecated(since C++11) if T has a user-defined destructor or user-defined copy assignment operator.


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


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