[C + +] Auto type descriptor

Source: Internet
Author: User

We often assign the value of an expression to a variable, which requires a clear understanding of the type of the expression when the variable is declared. Sometimes it's a complicated thing to introduce the auto type specifier in C++11, which allows the compiler to parse the type that the expression belongs to. Of course, the auto variable must have an initial value so that the compiler can infer its type:

Double VAL1 = 1.1, Val2 = 2.2;auto item = val1 + val2;//item is double type

Using auto can also declare multiple variables in a single statement. Because a statement can have only one base data type, the initial basic types of all variables in the statement must be identical:

Auto I0 = 0, *pi = &i0;//OK, i0 is int type, p is int pointer auto sz = 0, pi = 3.14;//fail, sz and PI type are inconsistent

Composite type, const, and auto

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

Reference: Using references to actually use referenced objects, when a reference is used as initialization, the actual participation in the initialization is actually the value of the Reference object. At this point, the compiler will reference the type of the object as the type of auto:

int i = 0, &r = i;auto A = r;//A is a int,r is an alias of I, and I is an int

Auto generally ignores the top-level const and retains the underlying const, such as when the initial value is a pointer to a constant:

const int CI = i, &cr = Ci;auto B = ci;//B is a INT,CI top-level const is ignored auto C = cr;//C is a INT,CR top-level const is ignored by auto D = &i;// D is an int pointer auto E = &ci;//E is a pointer to an integer constant, and the address of the constant object is a lower-level const

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

Const auto F = CI;

You can also set the reference type to auto, the original initialization rule still applies, and the top-level const property of the initial value is preserved:

Auto &g = ci;//g is an integer constant reference, bound to Ciauto &h = 42;//error, cannot be a very reference bound literal value const Auto &J = 42;//OK, you can reference bound literals for constants

To define multiple variables in a single statement, the symbols & and * are only subordinate to one declarator, not part of the base data type, so the initial values must be of the same type:

Auto k = ci, &l = i;//k is int, l is int&auto &m = ci, *p = &ci;//m is a reference to an integral type constant, p is a pointer to an integer constant auto &n = i, *p2 = &ci;//error, N is a reference to an integral type, p2 is a pointer to an integer constant

Some additional examples:

const int i = 42;auto J = i;//J is intconst auto &k = i;//k is a constant reference to int i auto *p = &i;//p is a pointer to integer constant i const auto J2 = i , &k2 = i;//J2 is an integer constant, K2 is a constant reference to int i


[C + +] Auto type descriptor

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.