Explain the methods for explicitly default and deleted functions in C + + _c language

Source: Internet
Author: User
Tags protected constructor types of functions

In c++11, the default functions and deleted functions allow you to explicitly control whether special member functions are automatically generated. A deleted function can also provide you with a simple language to prevent problematic type elevation in the parameters of all types of functions (special member functions and normal member functions and non-member functions), which can result in unexpected function calls.
Benefits of functions and deleted functions that are explicitly set by default
In C + +, if a type does not declare itself, the compiler automatically generates a default constructor, a copy constructor, a copy assignment operator, and a destructor for that type. These functions are called special member functions that make a simple user-defined type in C + + behave like a struct in C. That is, you can create, copy, and destroy them without any extra coding. C++11 introduces the mobile semantics into the language and adds the move constructor and the move assignment operator to the list of special member functions that the compiler can automatically generate.
This is convenient for simple types, but complex types typically define one or more special member functions themselves, which prevents other special member functions from being automatically generated. Practice Operation:

    • If any of the constructors are declared explicitly, the default constructor is not generated automatically.
    • If a virtual destructor is declared explicitly, the default destructor is not generated automatically.
    • If you explicitly declare a move constructor or move an assignment operator:
    1. The copy constructor is not generated automatically.
    2. Replication assignment operators are not automatically generated.
    • If the copy constructor, the copy assignment operator, the move constructor, the move assignment operator, or the destructor function are explicitly declared:
    1. The move constructor is not generated automatically.
    2. The move assignment operator is not automatically generated.

Attention

In addition, the C++11 standard specifies the following additional rules:

    • If you explicitly declare a copy constructor or destructor, the automatic generation of the replication assignment operator is discarded.
    • If you explicitly declare a copy assignment operator or destructor, discard the automatic generation of the copy constructor.
    • In both cases, Visual Studio will continue to automatically generate the required functions implicitly and without warning.

The results of these rules may also leak into the object hierarchy. For example, if a base class cannot have a default constructor that can be invoked from a derived class for some reason-that is, a public or protected constructor that takes no arguments-then a class derived from the base class will not automatically generate its own default constructor.

These rules can complicate the implementation of content that should be straightforward, user-defined types, and common C + + conventions-for example, by copying constructors and copying assignment operators in private, without defining them, so that user-defined types are not replicable.

struct noncopyable
{
 noncopyable () {};

Private:
 noncopyable (const noncopyable&);
 noncopyable& operator= (const noncopyable&);


Before c++11, this code snippet is a custom form of a type that cannot be replicated. However, it has several problems:
The copy constructor must be declared privately to hide it, but because it is fully declared, it prevents the automatic generation of default constructors. If you need a default constructor, you must explicitly define one (even if it does not do anything).
The compiler treats the explicitly defined default constructor as important, even if it does not perform any action. It is less efficient than the automatically generated default constructor and will prevent Noncopyable from becoming the true POD type.
Although copy constructors and copy assignment operators are hidden in external code, member functions and noncopyable friends can still see and invoke them. If they are declared but undefined, calling them can result in a linker error.
Although this is a widely accepted practice, the intent is ambiguous unless you understand all the rules used to automatically generate special member functions.
In c++11, non-duplicated idioms can be implemented in a more direct way.

struct noncopyable
{
 noncopyable () =default;
 Noncopyable (const noncopyable&) =delete;
 noncopyable& operator= (const noncopyable&) =delete;

Please note how to resolve issues related to the previous practice of c++11:
You can still prevent a default constructor from being generated by declaring a copy constructor, but you can restore it by explicitly setting it to the default value.
The default special member functions that are set explicitly are still considered unimportant, so performance does not degrade and does not prevent noncopyable from becoming the true POD type.
The copy constructor and the copy assignment operator are public, but have been deleted. Defining or calling a deleted function is a compile-time error.
For those who know =default and =delete, the intention is very clear. You don't have to understand the rules used to automatically generate special member functions.
Similar conventions exist for creating user-defined types that are not removable, can be dynamically assigned, or cannot be dynamically allocated. All of these conventions have C++11 implementations that suffer similar problems and can be resolved in a similar way in c++11 by implementing them by default and by removing special member functions.
functions with an explicit default setting
you can set any special member functions by default-explicitly declaring special member functions using the default implementation, defining special member functions with non-public access qualifiers, or recovering special member functions that are blocked from being automatically generated in other cases.
You can set special member functions by default by declaring them as shown in this example:

struct widget
{
 widget () =default;

 Inline widget& operator= (const widget&);


Inline widget& widget::operator= (const widget&) =default;

Note that as long as a special member function can be inline, it can be set by default outside the class body.
Because of the performance advantages of ordinary special member functions, we recommend that you prefer a special member function that is automatically generated when the default behavior is required instead of an empty function body. You can do this by explicitly setting the special member function by default, or by not declaring it (and not declaring other special member functions that will prevent it from being automatically generated).
attention
Visual Studio does not support default move constructors or move assignment operators as c++11 standard authorizations. For more information, see the "Default functions and Deleted Functions" section in support of C++11/14/17 features (modern C + +).
deleted functions
You can delete special member functions as well as ordinary member functions and non-members functions to prevent them from being defined or invoked. By removing special member functions, you can more succinctly prevent the compiler from generating special member functions that you do not need. You must delete a function when it is declared, and you cannot delete it after that by declaring a function and then no longer using it.

struct widget
{
 //deleted operator new prevents widget from being dynamically.
 void* operator new (std::size_t) = delete;

Deleting a normal member function or a non-member function prevents problematic type elevation from causing an unexpected function to be invoked. This works because the deleted function still participates in overload resolution and provides a better match for functions that might be called after the promoted type. The function call resolves to a more specific but removable function and causes a compiler error.

Deleted overload prevents call through type promotion of float to double from succeeding.
void Call_with_true_double_only (float) =delete;
void Call_with_true_double_only (double param) {return;}

Note that in the previous example, calling float with the call_with_true_double_only parameter would cause a compiler error, but calling int with the call_with_true_double_only argument would not cause a compiler error; example, this parameter is promoted from int to double, and the double version of the function is successfully invoked, even though this may not be intended. To ensure that any invocation of this function with a non-double-precision parameter causes a compiler error, you can declare the template version of the deleted function.

Template < typename T >


void call_with_true_double_only (t) =delete;//prevent Call through type promotion T to double from succeeding.

void Call_with_true_double_only (double param) {return;}//also define for const double, double&, etc. as needed.

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.