C ++ 11 standard new features: Defaulted and Deleted functions, defaulteddeleted
Reference: http://www.ibm.com/developerworks/cn/aix/library/1212_lufang_c11new/#ibm-pcon
The C ++ 11 standard has two new features: defaulted and deleted functions. For the defaulted function, the compiler automatically generates the default Function Definition body for it to achieve higher code execution efficiency. It can also help programmers manually define the function. For the deleted function, the compiler will disable it to avoid some illegal function calls or type conversion, thus improving the code security.
C ++ has four types of special member functions: default constructor, destructor, copy constructor, and copy value assignment operator. Special member functions of these classes are responsible for creating, initializing, and destroying, or copying class objects. If the programmer does not explicitly define a special member function for a class and needs to use this special member function, the compiler will implicitly generate a default special member function for this class.
Proposal of the defaulted Function
1. Reduce the programmer's programming workload; 2. Obtain the high code execution efficiency of the default special member functions automatically generated by the compiler.
Programmers only need to add"=default;
", You can declare this function as the defaulted function, and the compiler will automatically generate the function body for the explicitly declared defaulted function.
Proposal of the deleted Function
To enable the programmer to explicitly disable a function, the C ++ 11 standard introduces a new feature: the deleted function. Programmers only need to add"=delete;
To disable the function.
The deleted function feature can also be used to disable some conversion constructors of classes to avoid unexpected type conversion.
The deleted function feature can also be used to disable some user-defined classes.new
To avoid creating class objects in the free storage area.
Note:
Only four special member functions can be declared as defaulted functions, but the deleted function does not have this restriction.