C++ Reference ยท Keywords and Statements
Defaulted and deleted functions
Reference for C++ = default and = delete: requesting compiler-generated special member functions, forbidding copies and conversions, deleting overloads, and the rule of five.
Two special function bodies, both added in C++11. = default asks the compiler to generate the standard implementation of a special member function (constructor, destructor, copy and move operations, or in C++20 comparison operators). = delete makes a function exist but be unusable: calling it is a compile-time error.
Together they let a class state its design plainly: "copyable", "move-only", "not constructible from double", without tricks like declaring private functions with no definition.
struct Token { Token() = default; // keep the generated default constructor Token(const Token&) = delete; // not copyable Token(Token&&) noexcept = default; // movable Token& operator=(Token&&) noexcept = default; };
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.
Syntax
Class() = default; // special member functions Class(const Class&) = default; Class& operator=(const Class&) = default; Class(Class&&) noexcept = default; Class& operator=(Class&&) noexcept = default; ~Class() = default; bool operator==(const Class&) const = default; // C++20 auto operator<=>(const Class&) const = default; // C++20 return-type function(params) = delete; // any function, or a template specialization
Errors: calling a deleted function is a compile-time error, and overload resolution still considers deleted functions (which is how setPercent(double) above blocks the conversion to int).
Usage notes
- Declare
= defaultin the class body to get an inline generated function, or on the out-of-line definition (Widget::Widget() = default;in the.cpp) to keep the definition out of the header. The second form makes the function user-provided, which changes trivial-ness and some initialization rules. - A defaulted function is defined as deleted automatically when it cannot be generated (for example, a class with a
constmember or a reference member has no copy assignment, and a move-only member makes the class move-only). - Declaring any of destructor, copy, or move operations affects which others are generated. Follow the rule of five (or rule of zero): if you write one, think about all five; this is why
= defaultis useful to say "I mean the generated one". = deleteon copy operations plus= defaulton move operations is the standard recipe for move-only resource owners (std::unique_ptrworks this way).- Deleted functions take part in overload resolution, so deleting an overload blocks a call that would otherwise convert, a stronger and clearer tool than a private declaration.
- You can delete a function template specialization (
template<> void f<bool>(bool) = delete;), or all templates that match a condition withrequires(C++20). - A function must be declared
= deleteon its first declaration, and a deleted function cannot be called, but it can be named indecltypeorsizeof. = defaultfor comparison operators (C++20) compares members in declaration order.