C ++ 11 standard new features: Defaulted and Deleted functions, defaulteddeleted

Source: Internet
Author: User

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.newTo 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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.