C++11 Standard new features: defaulted and Deleted functions

Source: Internet
Author: User

This article references: http://www.ibm.com/developerworks/cn/aix/library/1212_lufang_c11new/#ibm-pcon

Two new features of the C++11 standard: defaulted and deleted functions. For the defaulted function, the compiler automatically generates a default function definition body for it, which results in higher code execution efficiency, and eliminates the programmer's effort to manually define the function. For the deleted function, the compiler disables it, avoiding some illegal function calls or type conversions, which can improve the security of your code

C + + classes have four special member functions, namely: Default constructors, destructors, copy constructors, and copy assignment operators. The special member functions of these classes are responsible for creating, initializing, destroying, or copying the objects of the class. If a programmer does not explicitly define a special member function for a class and needs to use that special member function, the compiler implicitly generates a default special member function for the class.

The defaulted of the function

1. Reduce programmer's programming workload; 2. Gets the high code execution efficiency of the default special member function generated automatically by the compiler.

The programmer simply adds "" After the function declaration to =default; declare the function as a defaulted function, and the compiler will automatically generate the function body for the explicitly declared defaulted function.

The deleted of the function

To enable programmers to explicitly disable a function, the C++11 standard introduces a new feature: the deleted function. The programmer simply adds "" After the function declaration to =delete; disable the function.

The deleted function attribute can also be used to disable some conversion constructors of a class, thus avoiding undesirable type conversions.

The deleted function attribute can also be used to disable the operators of certain user-defined classes new , thereby avoiding the creation of objects in the free-store class.


Attention:

Only 4 special member functions can be declared as defaulted functions, and the deleted function attribute does not have this limitation

C++11 Standard new features: defaulted and Deleted functions

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.