Summary of usage of auto and Decltype

Source: Internet
Author: User

One, auto

1, the role of autoin general, when assigning the return value of an expression or function to an object, we must know the return type of the expression, but sometimes it is difficult or impossible to know the expression or the return type of the function. This time, We can use the Auto keyword to let the compiler help us parse the expression or the type the function belongs to. Like what:
Auto item = val1 + Val2;auto red = layercolor::create (color4b (255, 100, 100, 255), 200, 100);
if both Val1 and val2 are of type int, then item is also of type int, and if Val1 and Val2 are double types, then item is a double type. and the return value type of Create () can be complicated This makes our programming easier and easier 2, auto and constAuto ignores the top-level const and retains the underlying const. Example:
const int i = 5;auto a = i;        The variable i is the top-level const and is ignored, so the type of B is intauto B = &i;    The variable i is a constant, which is the underlying const for a constant address, so the type of B is a const int *
Therefore, if you want the inferred type to be the top-level const, then you need to precede auto with cosnt:
Const auto C = i;
3, auto and reference① If the expression is a reference type, then the type of auto is the type of the object that is referenced.
int i = 2, &ri = I;auto k = ri;            K is of type int, not reference type

② If you want to declare a reference, you have to add &, and if you want to declare it as a pointer, you can add * or no *

int i = 3;auto &refi = i;        Refi is a reference to an int type auto *P1 = &i;       At this point the inferred type is int, p1 is a pointer to int auto P2 = &i;        At this point the inferred type is int*, and P2 is a pointer to int

Two, Decltype1, the role of DecltypeDecltype Initializes an object simply to infer the type of the expression without using the value of the expression.
Decltype (func ()) sum = x; The type of sum is the type of the return value of the function func (), but this does not actually call the function func () int i = 0;decltype (i) j = 4; The type of i is int, so the type of J is also int
2, Decltype and constwhether it is the top-level const or the underlying const, Decltype will retain
const int i = 3;decltype (i) j = i; J's type and I are the same, both const INT
3, Decltype and reference ① if the expression is a reference type, then the type of Decltype is also a reference
const int i = 3, &j = I;decltype (j) k = 5; The type of k is const int &
② If the expression is a reference type, but you want to get the type that the reference points to, you need to modify the expression:
int i = 3, &r = I;decltype (r + 0) T = 5; This is the INT type
③ returns a reference type for a pointer dereference operation
int i = 3, j = 6, *p = &i;decltype (*p) C = j; C is a reference to the int type, and C and J are bound together
④ if the type of an expression is not a reference, but we need to infer the reference, then we can add a pair of parentheses, it becomes the reference type.
int i = 3;decltype ((i)) j = i; The type of J at this point is a reference to type int, and J and I are bound together

    

Summary of usage of auto and Decltype

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.