C++11 Auto and Decltype detailed

Source: Internet
Author: User

Turn from: here

A. Introduction to Auto

When programming, it is often necessary to pay the value of an expression to a variable, and it is necessary to know clearly what the variable is when declaring the variable. It's not that easy (especially in templates) to do that, but sometimes it doesn't. To solve this problem, the c++11 new standard introduces the auto type specifier, which allows the compiler to parse the type that the expression belongs to. is different from the original one that only corresponds to a specific type specifier (for example, int). Auto lets the compiler use the initial values for type deduction. To get the type that defines the variable, so the variable that auto defines must have an initial value.

[CPP]View PlainCopy
    1. The result of adding val_1 and val_2 can infer the type of item
    2. Auto item = Val_1 + val_2;  The //item type is initialized to the added type of Val_1 + val_2, and the value is the value added by Val_1+val_2.



The type of item here is inferred by the compiler's addition to the type of Val_1 and val_2 during compilation. If it is val_1 (int) + val_2 (double), then the type of item is double.

Using auto can also declare multiple variables in a single statement, because a declaration of rain gear can only have one basic data type, so the initial basic data type of all variables must be the same for the rain gear. Be sure to differentiate data types and type modifiers here!!

[CPP]View PlainCopy
    1. int i = 3;
    2. Auto A = I,&b = I,*c = &i;  //Correct: A Initializes a copy of I, B Initializes a reference to I, and C is a pointer to I.
    3. Auto Sz = 0, pi = 3.14;  //error, two variable types are different.


The auto type inferred by the compiler is sometimes not exactly the same as the type of the initial value, and the compiler will change the result type appropriately to make it more compliant with the initialization rules.

First, as we know, the use of references is actually a reference object, especially when the reference is used as the initial value, the actual participation in the initialization is actually the value of the Reference object. The compiler now refers to the type of the object as the type of auto:

[CPP]View PlainCopy
    1. int i = 0, &r = i;  //Define an integer I, and define the application of R for I.
    2. Auto A = R;  //A In this case is an integer whose value is the same as I at this time.


It can be seen that auto ignores references, and second, auto generally ignores the top-level const, but the underlying const is preserved, such as when the initial value is a pointer to a constant:

[CPP]View PlainCopy
    1. int i = 0;  
    2. const int ci = i, & Cr = ci;  //ci  is an integer constant,cr  is an integer constant reference    
    3. auto a = ci;      // a  is an integer,  the top-level const is ignored   
    4. auto b = cr;     // b  is an integer, The top-level const is ignored   
    5. auto c = &ci;    < span class= "comment" >// c  is an integer pointer.   
    6. auto d = &cr;     // d  is a pointer to an integer constant (so const becomes the underlying const for the constant object area address)   


If you want to infer that the auto type is a top-level const, you need to be clear:

[CPP]View PlainCopy
    1. Const auto F = CI;


You can also set the reference type to auto, at which point the original initialization rule still applies (the const used to reference the declaration is the underlying const):

[CPP]View PlainCopy
    1. Auto &g = CI;  //g is an integer constant reference, bound to CI.
    2. Auto &h = 42;  //Error: The initial value of the very reference must be an lvalue.
    3. Const auto &J = 42;   //Correct: a constant reference can be bound to a literal.



Two. Decltype Introduction

Sometimes we encounter this situation, we want to infer from the expression to define the type of the variable, but do not want to use the value of an expression to initialize the variable. It is also possible that the return type of the function is a value type of an expression. At these times, auto appears powerless, so C++11 introduces the second type specifier, Decltype, which is the function of selecting and returning the data type of the operand. In this procedure, the compiler simply parses the expression and gets its type, without actually evaluating the value of the expression.

[CPP]View PlainCopy
    1. Decltype (f ()) sum = x;  the type of the//sum is the return value type of the function f.

Here the compiler does not actually call the F function, but instead parses the return value of the F function as the definition type of sum.

Basically the role of Decltype and auto very similar, not listed. Another use for Decltype is the post-return type introduced in c++11.

Three. Decltype and Auto Differences

Decltype handles the top-level const and reference in a slightly different way than auto, and if the expression used by Decltype is a variable, DECLTYPE returns the type of the variable (including the top-level const and the reference).

[CPP]View PlainCopy
    1. const int ci = &CJ = ci;
    2. Decltype (CI) x = 0; //x type is const int
    3. Auto z = CI; //z type is int
    4. Decltype (CJ) y = x; //Y type is const int&
    5. Auto h = CJ; //h type is int


Decltype There are some noteworthy areas, let's take a look at the following code:

[CPP]View PlainCopy
  1. int i = *p, = &i, &r = i;
  2. Decltype (i) x1 = 0; //Because I is an int, so x1 is int
  3. Auto x2 = i; //Because I is an int, so x2 is int
  4. Decltype (r) y1 = i; //Because R is int&, so Y1 is int&
  5. Auto y2 = r; //Because R is int&, but auto ignores references, so y2 is int
  6. Decltype (r + 0) z1 = 0; //Because R + 0 is int, so z1 is int,
  7. Auto Z2 = r + 0; //Because R + 0 is int, so Z2 is int,
  8. Decltype (*p) h1 = i; //Here H1 is int&, reason behind
  9. Auto H2 = *p;  //H2 is int.


If the content of an expression is a dereference operation, Decltype will get the reference type. As we are familiar with, dereference pointers can be used to get pointers to objects, and can also assign values to this object. Therefore the result type of Decltype (*p) is int&.

Another important difference between decltype and auto is that the result type of decltype is closely related to the form of expression. There is a situation that requires special attention: for decltype expressions, if the variable name is prefixed with a pair of parentheses, the resulting type may be different from the time it is not bracketed. If Decltype is using a variable without parentheses, then the result is the type of the variable. But if you add one or more parentheses to the variable, the compiler treats the variable as an expression, and the variable is a special expression that can be used as an lvalue, so the Decltype returns the reference type:

[CPP]View PlainCopy
    1. int i = 42;
    2. Decltype (i) int type
    3. Decltype ((i)) int& type

Another point to note here is that the = assignment operator returns a reference to an lvalue. In other words, that means decltype (i = b) returns a reference of type I type. Take a closer look at the following code:

[CPP]View PlainCopy
  1. int main ()
  2. {
  3. int i = 42;
  4. Decltype (i = a) x = i;
  5. Auto y = i;
  6. auto& z = i;
  7. printf ("I x y Z at this time:%d%d%d%d\n", i,x,y,z);
  8. i--;
  9. printf ("I x y Z at this time:%d%d%d%d\n", I, X, Y, z);
  10. x--;
  11. printf ("I x y Z at this time:%d%d%d%d\n", I, X, Y, z);
  12. y--;
  13. printf ("I x y Z at this time:%d%d%d%d\n", I, X, Y, z);
  14. z--;
  15. printf ("I x y Z at this time:%d%d%d%d\n", I, X, Y, z);
  16. return 0;
  17. }


The result of the operation is:

I x y Z at this time: 42 42 42 42
I x y Z at this time: 41 41 42 41
I x y Z at this time: 40 40 42 40
I x y Z at this time: 40 40 41 40
I x y Z at this time: 39 39 41 39


As you can see from the code above and the results of the operation, the assignment statements in 1.decltype (i = 41) do not actually run. 2. Decltype (i = 41) is actually a int&, which means that x is actually a reference to I.

After understanding the auto and Decltype, in the process of use must distinguish between the two, to prevent the definition of const and non-const as well as the difference between reference and non-reference !!

C++11 Auto and Decltype detailed

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.