"Effective modern C + +" Reading notes Item 2 type deduction for auto

Source: Internet
Author: User

Attention:

Also to learn a

↑↑↑↑ Such a box of fragments is not from the original book , but my own understanding.

Item 2 Understand Auto Type deduction- AutoType deduction

Before C++11, theauto keyword has been used to declare variables for the automatic storage type, with virtually no practical role, status and export Keyword (used to export a template outside of the compilation unit, and is also canceled in c++11).

In the c++11,auto finally no waste material, finally have a similar to C # in the var keyword effect, you can automatically deduce the type of variables, you can play a few words :

var map = dictionary<string, list<string>> ();

When declaring a variable with auto , the declared type can correspond to the Paramtype example of Item1 :

Template<typename t>void f (paramtype param);   // Examples of Item1  ; Const Auto cx = x; const auto& rx = x;

where auto takes the place of the original T type parameter, such as the const auto& part constitutes the paramtype This part. It can also deduce the type that auto represents.

Similarly, the derivation ofauto , there are three kinds of situations:

    1. The entire type part (paramtype part) is a pointer or reference, but not a universal reference;
    2. The type section is a universal reference;
    3. The type part is not a pointer and is not a reference

Where Scenario 2 behaves the same as the type deduction in Item1 :

 - ; Const Auto cx = x; const auto& rx = X;auto&& uref1 = x;  // bind to Lvalue,uref1 to int&  // bind to const lvalue,uref2 to const int&   //  bind to Rvalue,uref3 to int&& 

The universal reference is used on type expressions that do not have a type, require a type deduction, specify that the type is a reference, and then the compiler automatically infers the most appropriate reference type that conforms to the rule.

In addition, the degradation of array names and function names can also be avoided with auto& :

Const char name[] ="R. N. Briggs";  // Type:const char[13]  = name;  // Const char* // non-degenerate: const char (&) [+]

AutoSyntax points to be aware of

C + + Adds a new initialization syntax, which is to initialize with {} . But C++11 also added the initializer_list template, which makes it important to use auto for type deduction:

;     // X1 is an intauto x2();      // ditto, through () Initialize  - // The x3 is std::initializer_list<int> the value is {+}  +};    // Ibid .

The curly Brace Initialization list (braced-init-list) is constructed as a initializer_list in auto and for loops with colons (range-based for loop) .

Reference Http://en.cppreference.com/w/cpp/utility/initializer_list

At the same time, different types of variables can cause initializer_list<t> to fail the type deduction of T :

 123.0//  Error

X5 because auto is deduced as initializer_list<t>, which leads to the failure of the deduction of T .

The processing of curly brace initializers is the only difference between the auto and the template on type deduction:

 9 };  // OK Template void  9 });        // T

However, you can indicate that the type of param is initializer_list:

Template<typename t>void f (std::initializer_list<t>9  //  T, int, paramtype-  std::initializer_list<int>

Auto assumes that the curly brace initial list is initializer_list, and the template does not make such assumptions.

In c++14, auto keyword can be used for function return value deduction, and for Lambda The formal parameter type declaration of an expression. In both of these usages, keyword implements the template type deduction rule instead of the auto type deduction rule, which is at this time the auto cannot be deduced as   initializer_list :

  auto createinitlist () { return  {1 , 2 ,  3 }; //    Auto  deduces The return value and fails   }std::vector  <int  > V;auto Resetv  = [&v] (const  auto& newvalue) {v = newvalue;}; //    Auto  is used to declare  lambda  expression parameter types   Resetv ({1 , 2 , 3 }); //   similarly failed  

Note Points for this article
    1. Auto The type deduction rules are basically the same as the template type deduction rules, the only difference being that auto assumes curly braces represent initializer_list.
    2. The auto keyword executes the template type deduction rule when the return value and the lambda expression parameter deduction.

"Effective modern C + +" Reading notes Item 2 type deduction for auto

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.