§The Annotated C++ StandardC++ 标准注疏 中文
11.4.4

Special member functions

The six member functions the compiler can declare and define for a class: when they are implicitly declared, when they are defined as deleted, when they are actually generated, and how C++20 uses constraints to make them conditionally trivial.

Key points

  • There are six special member functions: the default constructor, copy constructor, move constructor, copy assignment, move assignment and destructor. When the program doesn't declare them, the compiler declares them for you at the } of the class definition.
  • Declaring one special member function yourself affects the others: declaring a destructor or a copy operation stops the move operations from being implicitly declared, and declaring a move operation makes the copy operations deleted.
  • When the move operations aren't declared, rvalues fall back to the copy constructor. The program still compiles; it just quietly copies.
  • If the compiler-generated version would be ill-formed (say, assignment for a class with a reference member), it is defined as deleted. The class is still usable; only actually using that operation is an error.
  • Since C++20 you can write several versions of the same special member function with requires. The constraints pick the eligible one, so a class template can be trivial or not depending on its template arguments.
C++✓ well-formedgodbolt ↗
#include <string>
struct Person {
  std::string name;
  int age;
};
int main() {
  Person a{"Ada", 36};
  Person b = a;             // copy construction
  Person c = std::move(a);  // move construction
  b = c;                    // copy assignment
  c = Person{"Bob", 1};     // move assignment
}                           // destruction

Person has no member functions written, yet it can be copied, moved, assigned and destroyed. The compiler declares and defines these operations for it, and such member functions are called special member functions (special member function). This page covers the general rules for them; the details of each are in its own subclause.

Special member function Typical signature Details
Default constructor X() [class.default.ctor]
Copy constructor X(const X&) [class.copy.ctor]
Move constructor X(X&&) [class.copy.ctor]
Copy assignment operator X& operator=(const X&) [class.copy.assign]
Move assignment operator X& operator=(X&&) [class.copy.assign]
Destructor ~X() [class.dtor]

Converting constructors and conversion functions ([class.conv]) are not special member functions. Defaulted comparison operators ([class.compare.default]) can also be written = default, but they aren't special member functions either.

What the compiler declares for you (¶1–¶4)#

¶1 In the standard's list, the destructor slot actually says prospective destructor (prospective destructor). A class can declare several prospective destructors, and one of them is eventually selected as the real destructor; see the section on constrained special member functions below.

When the program doesn't declare a special member function, the compiler implicitly declares it for some classes, and implicitly defines it when needed. Two points in time have to be kept apart:

  • The implicit declaration happens at the closing brace } of the class definition. So the functions aren't visible yet in the class's member declarations, but they can be used in member function bodies, because those are processed only after the } ([class.mem]/10).
  • The implicit definition, that is, actually generating the function body, waits until the function is used; see below.

A program cannot define an implicitly-declared special member function. To provide your own definition, you must first declare it in the class:

C++no-define-implicit.cpp✗ ill-formed
struct S { int x; };
S::S() : x(0) { }   // error: S::S() is implicitly declared

Two terms: defaulted and deleted#

A defaulted function is one whose definition is left to the compiler: implicitly-declared special member functions, and functions written with = default ([dcl.fct.def.default]/5). A deleted function has a declaration, but any code that uses it is ill-formed ([dcl.fct.def.delete]). A deleted function still takes part in overload resolution; it is an error only if it gets selected.

A defaulted function may be defined as deleted by the compiler, which comes up repeatedly below. Also, both = default and = delete count as user-declared (user-declared), even though the body is generated by the compiler.

Implicitly-declared functions can be called explicitly (¶2)#

¶2 Implicitly-declared special member functions can be called explicitly, and you can form pointers to them, just like ordinary member functions:

C++call-implicit.cpp✓ well-formedgodbolt ↗
struct A { int a = 1; };     // implicitly declares A::operator=
struct B : A {
  int b = 2;
  B& operator=(const B& s) {
    this->A::operator=(s);   // OK: calls the implicitly-declared version
    b = s.b;
    return *this;
  }
};
int main() {
  B x, y;
  y.a = 10;
  x = y;
  A& (A::*pm)(const A&) = &A::operator=;  // OK
  (void)pm;
  return x.a - 10;
}

¶4 Special member functions obey the usual access rules ([class.access]). Declaring the constructors protected means only derived classes and friends can create objects, which is the usual way to write a base class meant only for inheritance.

Declare one, affect the others#

The most frequently consulted table in C++ programming: what happens to the others once the user declares one special member function. Rows are the user-declared function, columns are what the compiler does with each of the others.

User declares ↓ / implicit → Default ctor Destructor Copy ctor Copy assign Move ctor Move assign
Nothing defaulted defaulted defaulted defaulted defaulted defaulted
Some other constructor not declared defaulted defaulted defaulted defaulted defaulted
Default constructor defaulted defaulted defaulted defaulted defaulted
Destructor defaulted defaulted (deprecated) defaulted (deprecated) not declared not declared
Copy constructor not declared defaulted defaulted (deprecated) not declared not declared
Copy assignment operator defaulted defaulted defaulted (deprecated) not declared not declared
Move constructor not declared defaulted deleted deleted not declared
Move assignment operator defaulted defaulted deleted deleted not declared

Each column comes from one sentence in the corresponding subclause:

  • Default constructor: implicitly declared only if the class has no user-declared constructor at all (including copy and move constructors) ([class.default.ctor]/1).
  • Destructor: implicitly declared if there is no user-declared prospective destructor; no other declaration affects it ([class.dtor]/2).
  • Copy constructor: implicitly declared if there is no user-declared copy constructor. It is defined as deleted if the class declares a move operation; if the class has a user-declared copy assignment operator or destructor, the implicitly-declared copy constructor is deprecated ([class.copy.ctor]/6, [depr.impldec]).
  • Copy assignment operator: the rule is symmetric ([class.copy.assign]/2).
  • Move constructor: implicitly declared only if the class has no user-declared copy constructor, copy assignment operator, move assignment operator or destructor ([class.copy.ctor]/8).
  • Move assignment operator: implicitly declared only if the class has no user-declared copy constructor, move constructor, copy assignment operator or destructor ([class.copy.assign]/4).

Not declared is not the same as deleted#

C++✓ well-formedgodbolt ↗
struct D {
  ~D() { }  // move operations not declared
};
int main() {
  D a;
  D b = static_cast<D&&>(a);
  // OK: uses the copy constructor
}
C++✗ ill-formed
struct M {
  M() = default;
  M(M&&) = default;
  // copy constructor: deleted
};
int main() {
  M a;
  M b = a;  // error
}

D on the left has no move constructor. When it is initialized from an rvalue, overload resolution finds the copy constructor D(const D&); a const lvalue reference can bind to an rvalue, so the program compiles as usual, and the move quietly becomes a copy.

M on the right declares a move constructor, so its copy constructor is defined as deleted. A deleted function still takes part in overload resolution; it is selected when initializing from an lvalue, and the program is ill-formed. This is how std::unique_ptr becomes non-copyable.

Here are a few cells of the table checked with type traits:

C++special-table.cpp✓ well-formedgodbolt ↗
#include <type_traits>
using namespace std;

struct Dtor { ~Dtor() { } };
static_assert(is_copy_constructible_v<Dtor>);
static_assert(is_move_constructible_v<Dtor>);  // actually uses the copy constructor
static_assert(!is_trivially_move_constructible_v<Dtor>);

struct CC { CC(const CC&) = default; };
static_assert(!is_default_constructible_v<CC>);

struct MC { MC(MC&&) = default; };
static_assert(!is_copy_constructible_v<MC>);   // deleted
static_assert(!is_copy_assignable_v<MC>);      // deleted
static_assert(!is_move_assignable_v<MC>);      // move assignment not declared, copy assignment deleted

struct MA { MA& operator=(MA&&) = default; };
static_assert(is_default_constructible_v<MA>);
static_assert(!is_move_constructible_v<MA>);

int main() { }

is_move_constructible_v<Dtor> being true only says the type can be constructed from an rvalue, not that the move constructor is what gets called.

What follows for everyday code#

  • Rule of zero: don't write them if you don't have to. Let members like std::string, std::vector and std::unique_ptr manage resources, and leave all six special member functions to the compiler; they are automatically correct, and trivial and noexcept when possible.
  • Rule of five: once you need to write any one of the destructor, copy or move operations yourself, declare all five explicitly, using = default or = delete where appropriate. Writing only a destructor makes the move operations disappear, and the implicit copies are deprecated.
  • The four cells marked deprecated in the table were kept by C++11 for compatibility with C++98 code. [depr.impldec] says they may be changed to deleted in the future.

When a defaulted function is defined as deleted#

C++defaulted-deleted.cpp✗ ill-formed
struct R { int& r; };       // has a reference member
struct C { const int c; };  // has a const member

void f(R& a, R& b, C& x, C& y) {
  R r;        // error: default constructor is defined as deleted
  C c;        // error: likewise
  a = b;      // error: copy assignment is defined as deleted
  x = y;      // error: likewise
  R r2 = a;   // OK: copy construction is fine
}

The definitions of R and C are not themselves wrong. The default constructor the compiler would generate can't initialize a reference or a const int, and the generated assignment operator can't rebind a reference or modify a const member. The standard handles this by defining those defaulted functions as deleted rather than making the class definition ill-formed. The class remains usable, and only actually calling those operations is an error.

Potentially constructed subobjects (¶7)#

The various deletion conditions all have to check subobjects: whether members and bases can be constructed and destroyed. The set of subobjects checked has a name.

¶7 A class's direct non-static data members, its non-virtual direct base classes, and, if the class is not abstract, its virtual base classes are together called its potentially constructed subobjects (potentially constructed subobjects).

Abstract classes exclude virtual bases because a virtual base is initialized only by the constructor of the most derived class ([class.base.init]). An abstract class can't have complete objects, so it is never the most derived class and its constructor never actually constructs the virtual base. So even if the virtual base has no default constructor, there is no reason to delete the abstract class's default constructor:

C++abstract-vbase.cpp✓ well-formedgodbolt ↗
struct V { V(int) { } };   // no default constructor
struct A : virtual V {     // abstract class
  virtual void f() = 0;
};                         // A() is not deleted because of V
struct C : A {
  C() : V(42) { }          // the most derived class constructs the virtual base
  void f() override { }
};
int main() { C c; (void)c; }

Deletion conditions at a glance#

The table below collects the deletion conditions from the individual subclauses; see each page for details.

Defaulted function Main conditions for being deleted
Default constructor A reference member without a default member initializer; a const member of a non-union class with no initializer whose type can't be default-initialized to a determinate value; a potentially constructed subobject without a usable default constructor; a subobject whose destructor is deleted or inaccessible ([class.default.ctor]/2)
Copy/move constructor A potentially constructed subobject without a usable corresponding constructor, or a variant member (a member of a union) whose corresponding function is non-trivial; a subobject whose destructor is deleted or inaccessible; a copy constructor with an rvalue reference member ([class.copy.ctor]/10)
Copy/move assignment operator A const member of non-class type; a reference member; a direct member or direct base without a usable corresponding assignment operator, or a variant member whose corresponding function is non-trivial ([class.copy.assign]/7)
Destructor In a non-union class, a potentially constructed subobject whose destructor is deleted or inaccessible; several cases for unions; a virtual destructor with no usable operator delete ([class.dtor]/7)
Any of them Not an eligible special member function (see the next section), or a prospective destructor that wasn't selected ([dcl.fct.def.default]/5)

Deleted defaulted move operations are ignored by overload resolution#

If a member can be copied but not moved, the class's defaulted move constructor is defined as deleted. Under the general rule for deleted functions, it would be selected when initializing from an rvalue and then be an error, even though the copy constructor could do the job perfectly well.

So the standard has an extra rule: a defaulted move constructor or move assignment operator that is defined as deleted is ignored by overload resolution ([class.copy.ctor]/10 note 4, [class.copy.assign]/7 note 4). Move operations explicitly written = delete are not affected by this and still take part in overload resolution.

C++deleted-move-ignored.cpp✓ well-formedgodbolt ↗
#include <utility>

struct NM {                 // copyable, move explicitly deleted
  NM() { }
  NM(const NM&) { }
  NM(NM&&) = delete;
};

struct Y {
  NM n;
  Y() = default;
  Y(const Y&) = default;
  Y(Y&&) = default;         // defined as deleted, and ignored
};

int main() {
  Y a;
  Y b = std::move(a);       // OK: selects Y(const Y&)
  (void)b;
  NM m;
  // NM m2 = std::move(m);  // error: selects the explicitly deleted NM(NM&&)
}

Clang gives a -Wdefaulted-function-deleted warning on Y(Y&&) = default;, pointing out that it is actually deleted.

Constrained special member functions (¶5–¶6) C++20#

std::optional<int> should be trivially copyable, and std::optional<std::string> can't be. Before C++20, a class template could only achieve this by splitting its implementation across several layers of base classes and handling each case with partial specializations. C++20 allows several constrained versions in the same class:

C++conditional-trivial.cpp✓ well-formedgodbolt ↗
#include <string>
#include <type_traits>

template<class T>
struct Box {
  T value;
  Box(T v) : value(v) { }
  Box(const Box&)
    requires std::is_trivially_copy_constructible_v<T>
    = default;                                    // #1
  Box(const Box& o) : value(o.value) { }          // #2
};

static_assert(std::is_trivially_copy_constructible_v<Box<int>>);
static_assert(!std::is_trivially_copy_constructible_v<Box<std::string>>);

int main() { Box<std::string> a{"x"}; Box<std::string> b = a; (void)b; }

In Box<int>, the constraint of #1 is satisfied, and #1 is more constrained (more constrained, compared per [temp.constr.order]) than the unconstrained #2, so #1 is eligible and #2 is not. Whether the class is trivially copyable looks only at the eligible functions, so Box<int> is trivially copyable. In Box<std::string> the constraint of #1 isn't satisfied, leaving only #2.

Same kind and eligible#

Which one is more constrained is only compared between functions of the same kind. Paragraph 5 says two special member functions are of the same kind if:

  • ¶5.1 they are both default constructors,
  • ¶5.2 they are both copy or move constructors with the same first parameter type, or
  • ¶5.3 they are both copy or move assignment operators with the same first parameter type and the same cv-qualifiers and ref-qualifier, if any.

So X(const X&) and X(X&) are not of the same kind, and both can be eligible at once.

¶6 An eligible special member function (eligible special member function) meets three conditions:

  • ¶6.1 it is not deleted,
  • ¶6.2 its associated constraints, if any, are satisfied, and
  • ¶6.3 no special member function of the same kind whose constraints are satisfied is more constrained.

Eligibility is used in several places: a trivially copyable class looks only at the eligible copy and move operations and destructor ([class.prop]/1); an implicit-lifetime class requires at least one eligible trivial constructor ([class.prop]/8); and a defaulted special member function that isn't eligible is defined as deleted ([dcl.fct.def.default]/5).

Prospective destructors#

A destructor has no parameters and can't be overloaded, so multiple constrained versions need another mechanism. C++20 lets a class declare several prospective destructors, and at the end of the class definition, overload resolution with an empty argument list selects the real destructor ([class.dtor]/4):

C++prospective-dtor.cpp✓ well-formedgodbolt ↗
#include <string>
#include <type_traits>

template<class T>
struct Holder {
  ~Holder()
    requires std::is_trivially_destructible_v<T>
    = default;                    // #1
  ~Holder() { /* non-trivial cleanup */ }  // #2
  T t;
};

static_assert(std::is_trivially_destructible_v<Holder<int>>);
static_assert(!std::is_trivially_destructible_v<Holder<std::string>>);

int main() { Holder<std::string> h{"x"}; (void)h; }

std::optional, std::variant and std::expected in the major standard libraries all use this mechanism to provide trivial and non-trivial versions in a single class template.

When a defaulted function is actually generated#

The compiler doesn't generate six function bodies for every class. [dcl.fct.def.default]/5 splits defaulted functions into two kinds:

  • Those that are implicitly declared, or written = default on their first declaration in the class, are not user-provided (user-provided). If they aren't defined as deleted, they are implicitly defined only when odr-used or needed for constant evaluation.
  • One that is declared first and then written = default outside the class is user-provided. It is defined at the point of the = default, and since it is user-provided, it is not trivial.
C++user-provided-default.cpp✓ well-formedgodbolt ↗
#include <type_traits>

struct A { A() = default; };  // not user-provided: can be trivial
struct B { B(); };
B::B() = default;             // user-provided: not trivial

static_assert(std::is_trivially_default_constructible_v<A>);
static_assert(!std::is_trivially_default_constructible_v<B>);

int main() { }

The point of an out-of-class = default is to keep the definition in the .cpp file, so that when the class's implementation changes, the header and the binary interface don't ([dcl.fct.def.default]/5 note 2). The destructor of a pimpl class is often written this way: declare ~Widget(); in the header, and write Widget::~Widget() = default; in the .cpp file where Impl is complete.

Implicitly-defined special member functions are all constexpr ([class.default.ctor]/4, [class.copy.ctor]/13, [class.copy.assign]/10, [class.dtor]/9). Whether one can be called during constant evaluation still depends on whether what it actually does meets the requirements for constant expressions.

Implementation view#

Lazy generation

Implicit definition happens only when the function is odr-used. So a class with a non-copyable member doesn't produce an error from a failed copy constructor as long as it is never copied. With clang++ -Xclang -ast-dump you can see that implicitly-declared members are marked implicit in the AST.

Triviality and calling conventions

Trivial copies and moves compile to memcpy or register copies, and trivial default construction and destruction generate no code at all. In the Itanium C++ ABI (GCC and Clang on Linux and macOS), a class with a non-trivial copy constructor, move constructor or destructor, or one whose copy and move constructors are all deleted, can't be passed or returned in registers; it goes through a hidden pointer. Adding an empty destructor to a class can change its calling convention.

History#

History

  • C++98 had four special member functions: default constructor, copy constructor, copy assignment and destructor. If the user didn't declare them, the compiler declared them implicitly.
  • C++11 added the move constructor and move assignment and introduced = default and = delete, producing the table above. The other copy operation is still implicitly declared when the destructor or a copy operation is declared, so as not to break C++98 code, and is only marked deprecated.
  • C++20 introduced eligibility, prospective destructors and conditional triviality (P0848R3), and destructors can be constexpr.
  • C++23 allows a defaulted copy assignment operator to use an explicit object parameter ([dcl.fct.def.default]/2).
  • In C++26, the default constructor and destructor of a union are no longer deleted just because a variant member is non-trivial. The deprecated implicit copies have still not been changed to deleted.

Common misconceptions#

Adding just a destructor quietly costs performance

In struct Big { std::vector<int> v; ~Big() { log(); } };, the user-declared destructor stops the move operations from being implicitly declared. Growing a std::vector<Big> or returning by value from a function (when the copy can't be elided) both copy the whole v. The fix is to add Big(Big&&) = default; and Big& operator=(Big&&) = default;, and also explicitly default the copy operations, since otherwise declaring the move operations deletes them.

Assuming = default is always usable

= default only asks the compiler to generate the function according to the rules. If the rules call for deletion, as with copy assignment for a class with a reference member, it is deleted. Clang's -Wdefaulted-function-deleted, enabled by default, flags this.

Assuming a constructor template suppresses the implicit copy constructor

A template is never a copy or move constructor ([class.copy.ctor]/1), so template<class T> X(T&&); doesn't prevent X(const X&) from being implicitly declared. But for a non-const lvalue the template is a better match than X(const X&) and hijacks the copy; see [class.copy.ctor].

The const on a defaulted function's parameter doesn't match the implicit version

[dcl.fct.def.default]/2 allows an explicitly-defaulted special member function to differ from the implicit version in only a few ways. When the implicit version's parameter is const C&, you may default a version whose parameter is C& ([dcl.fct.def.default]/2.4). The other way round, when the implicit version is C& (because some member has only a copy constructor like M(M&)) and you default a const C& version: if it is defaulted on its first declaration it is defined as deleted; if it is defaulted outside the class, the program is ill-formed ([dcl.fct.def.default]/2.5).

Questions#

Question 1

Which special member functions does this class have, and which are usable?

C++
#include <string>
struct S {
  S(int);
  S& operator=(const S&) = default;
  std::string s;
};
Answer
  • Default constructor: there is a user-declared constructor S(int), so it isn't declared.
  • Copy constructor: implicitly declared as defaulted, but because there is a user-declared copy assignment operator this case is deprecated, and Clang's -Wdeprecated-copy warns about it.
  • Copy assignment: user-declared, defaulted.
  • Move constructor, move assignment: there is a user-declared copy assignment operator, so neither is declared. S b = std::move(a); calls the copy constructor, and s is copied rather than moved.
  • Destructor: implicitly declared as defaulted.

Question 2

Declaring a destructor doesn't delete the copy operations, but declaring a move operation does. Why is C++11 asymmetric here?

Answer

Compatibility with old code. C++98 code has many classes that declare only a destructor, sometimes one that just logs. If C++11 had deleted their copy operations, that code would stop compiling, so they could only be deprecated. Move operations were new in C++11, so a class that declares one is necessarily new code whose author is thinking about resource semantics; there, a memberwise copy is quite likely wrong (a shallow copy of an exclusively owned resource, for example), and deleting it by default breaks no old code.

Question 3

After struct A { int a; };, does auto pm = &A::operator=; compile?

Answer

No. A implicitly declares two operator=: the copy assignment A& operator=(const A&) and the move assignment A& operator=(A&&). &A::operator= is an overload set, and auto can't deduce a single type from it. You have to give the target type, as call-implicit.cpp does: A& (A::*pm)(const A&) = &A::operator=;. Paragraph 2 says you can form a pointer to an implicitly-declared function; the rules are the same as for taking the address of an ordinary overloaded function ([over.over]).

Summary#

Concept Key points
Special member functions Default constructor, copy constructor, move constructor, copy assignment, move assignment, (prospective) destructor
Implicit declaration At the } of the class definition; a program can't define an implicitly-declared function
Interactions Any constructor suppresses the default constructor; a copy operation or destructor suppresses the move operations; a move operation makes the copy operations deleted; a copy operation or destructor makes the other copy operation deprecated
Deleted Defined as deleted when the generated version would be ill-formed; deleted defaulted move operations are ignored by overload resolution
Potentially constructed subobjects Direct data members, non-virtual direct bases, and virtual bases of a non-abstract class
Eligible Not deleted, constraints satisfied, no more-constrained function of the same kind; decides properties like trivially copyable
Prospective destructors There can be several; overload resolution selects the destructor at the end of the class definition
Implicit definition Generated only when odr-used or needed for constant evaluation; an out-of-class = default is user-provided and not trivial; implicitly-defined ones are all constexpr
Remaining wording

Note 1 in ¶1 says the implementation will implicitly declare special member functions for some classes and implicitly define them when needed ([dcl.fct.def.default]). Note 2 in ¶3 says special member functions affect the way class objects are created, copied, moved and destroyed, and how values can be converted to values of other types, and that they are often called implicitly.

The authoritative text is eel.is/c++draft/special (working draft 2026-08-23, commit c7015b485c). These are study notes that explain rather than quote; ¶ numbers follow the draft. Translated from the Chinese original. Last updated 2026-10-19.