10

Today I stumbled over a code snippet like this one:

class A 
{
    A() = default;
    A (const A&) = delete; 

    ...
}

I've never seen either the delete or default keyword. Are they part of C++11 std? And what are they used for?

Morwenn
  • 21,684
  • 12
  • 93
  • 152
djf
  • 6,592
  • 6
  • 44
  • 62
  • They are features introduced in C++11. They allow explicitly disabling use of the function or directing the compiler to use the default constructor. Check [here](http://en.wikipedia.org/wiki/C%2B%2B11#Explicitly_defaulted_and_deleted_special_member_functions). – Alok Save May 27 '13 at 09:50
  • yes they are. 8.4.2 and 8.4.3 – Koushik Shetty May 27 '13 at 09:51
  • 1
    [And here are some examples](http://stackoverflow.com/a/7234700/1364752). – Morwenn May 27 '13 at 09:52
  • possible duplicate of [What's the point in defaulting functions in C++11?](http://stackoverflow.com/questions/823935/whats-the-point-in-defaulting-functions-in-c11) – stijn May 27 '13 at 10:05
  • @Morwenn +1 for the examples – djf May 27 '13 at 10:27

1 Answers1

12

Special member functions can now be defaulted or deleted.

A deleted member function still takes part in overload resolution, but if it gets chosen, the program is ill-formed and compilation stops with a useful diagnostic. This is The Right Way to write things like non-copyable classes, and the user gets a proper error message.

A defaulted member function "does what it should", e.g. a defaulted default constructor default-initializes all bases and members and has empty body; a defaulted copy constructor copies each base and member object, and a defaulted assignment operator assigns each base and member object. If any of those operations aren't allowed (e.g. you have reference members), then the defaulted member function is defined as deleted.

Note that your first declaration-definition A() = default; makes the constructor A::A() user-declared but not user-defined; this is important for the classification of A, e.g. whether it is POD. (And notice that this is different from struct A { A(); }; A::A() = default; which is user-defined.)

Another nice consequence is the clarification of implicitly generated things: If you don't write certain functions yourself at all (like copy constructors), one gets implicitly declared for you. When the implicitly-declared one is odr-used, it gets implicitly defined as defaulted, and thus if it's not possible (e.g. if the class has non-copyable members), it actually gets implicitly defined as deleted. So that's generally a neat way of propagating things like non-copyability and non-assignability, at least in terms of the language and the consequent diagnostics.

Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084
  • 1
    I think you can actually default an assignment operator of a class with a reference-member - however, the assignment operator is then defined as "deleted" - the same thing that would happen if you let the compiler generate it. – Xeo May 27 '13 at 10:01
  • @Xeo: Ah, yes, that makes more sense. Thanks! Edited. – Kerrek SB May 27 '13 at 10:01