Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c++ syntax: default and delete modifiers

Tags:

c++

c++11

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?

like image 225
djf Avatar asked May 27 '13 09:05

djf


People also ask

What are the data type modifiers available in C?

Data type modifiers in C program are used with the Integer, Double and Character data types to modify the length of data that an Integer, Double or Character data type can hold. Data Type modifiers available in C are: signed - It is default modifier of int and char data type if no modifier is specified.

What is defaulted function in C++?

Defaulted Function. What is a Defaulted Function? Explicitly defaulted function declaration is a new form of function declaration that is introduced into the C++11 standard which allows you to append the ‘=default;’ specifier to the end of a function declaration to declare that function as an explicitly defaulted function.

What is the use of delete operator in C++?

The C++ 11 standard introduced another use of this operator, which is: To disable the usage of a member function. This is done by appending the =delete; specifier to the end of that function declaration. Any member function whose usage has been disabled by using the ‘=delete’ specifier is known as an explicitly deleted function.

How to disable the usage of a member function in C++?

The C++ 11 standard introduced another use of this operator, which is: To disable the usage of a member function. This is done by appending the =delete; specifier to the end of that function declaration.


1 Answers

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.

like image 125
Kerrek SB Avatar answered Oct 27 '22 04:10

Kerrek SB