Why you need to pay attention to these special member functions. Given
half a chance, the compiler will write them for you. Another reason is that
C++ by default treats classes as value-like types, but not all types are
value-like. Know when to write and disable them make you get correct
code.
A good reference is "Everything You ever wanted to know about move
semantics" in slideshare.net.
For these special member functions, main operations can be:
Compiler implicitly declare one
Use explicitly declare one
Once you define one, Compiler maybe Not declare another
You can ask compiler declare one
You can ask compiler delete one.
First question is what "declare" mean?
If you just define a class without any special member function, all six
member function will be declared by compiler implicitly.
Next question is what differences are between =default and user define
empty ctor? problems of below code snippet are:
The copy constructor has to be declared privately to hide it, but
because it’s declared at all, automatic generation of the default
constructor is prevented. You have to explicitly define the default
constructor if you want one, even if it does nothing.
Even if the explicitly-defined default constructor does nothing, it’s
considered non-trivial by the compiler. It’s less efficient than anautomatically generated default constructor and preventsnoncopyable from being a true POD type.
Even though the copy constructor and copy-assignment operator
are hidden from outside code, the member functions and friends
of noncopyable can still see and call them. If they are declared but
not defined, calling them causes a linker error.
Although this is a commonly accepted idiom, the intent is not clear
unless you understand all of the rules for automatic generation of
the special member functions.
C++11 new keyword default and delete give below advantages:
Generation of the default constructor is still prevented by
declaring the copy constructor, but you can bring it back by
explicitly defaulting it.
Explicitly defaulted special member functions are still considered
trivial, so there is no performance penalty, and noncopyable is not
prevented from being a true POD type.
The copy constructor and copy-assignment operator are public
but deleted. It is a compile-time error to define or call a deleted
function.
The intent is clear to anyone who understands =default and
=delete. You don’t have to understand the rules for automatic
generation of special member functions.
Another question is what differences are between =delete and "not declare"