[C ++] auto type specifier

Source: Internet
Author: User

[C ++] auto type specifier

We often assign the expression value to the variable, which requires that the type of the expression be clearly known During Variable declaration. Sometimes it is very complicated. The auto type specifier is introduced in C ++ 11, which allows the compiler to analyze the type of the expression for us. Of course, the auto variable must have an initial value so that the compiler can deduce its type:

 

Double val1 = 1.1, val2 = 2.2; auto item = val1 + val2; // item is of the double Type

You can also declare multiple variables in a statement using auto. Because a statement can only have one basic data type, the initial basic types of all variables in the statement must be consistent:

 

 

Auto i0 = 0, * pI = & i0; // OK, i0 is int type, p is int pointer auto sz = 0, pi = 3.14; // fail, the sz and pi types are inconsistent.

 

Composite Type, const, and auto

 

The auto type inferred by the compiler is sometimes different from the initial value type. The compiler will change the result type appropriately to make it more compliant with the initialization rules.

Reference: The reference actually uses the referenced object. When the reference is used for initialization, the value of the referenced object is actually involved in initialization. In this case, the compiler uses the type of the referenced object as the auto type:

 

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

Auto ignores the top-level const and retains the underlying const. For example, when the initial value is a pointer to a constant:

 

 

Const int ci = I, & cr = ci; auto B = ci; // B is an int, and the top-level const of ci is ignored. auto c = cr; // c is an int, and the top-level const of cr is ignored. auto d = & I; // d is an int pointer auto e = & ci; // e is a pointer to an integer constant. Getting the address of a constant object is a kind of underlying const.

If you want to infer that the auto type is a top-level const, You need to specify the expenditure:

 

 

const auto f = ci;

You can also set the reference type to auto. The original initialization rule still applies, and the top-level const attribute of the initial value will be retained:

 

 

Auto & g = ci; // g is an integer constant reference, bound to ciauto & h = 42; // error, the const auto & j = 42; // OK cannot be bound to a constant reference.

To define multiple variables in a statement, the symbols & and * must belong to a declaration rather than a part of the basic data type. Therefore, 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 integer constant. p is the pointer to an integer constant, auto & n = I, * p2 = & ci; // error. n is a reference to an integer, 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 of int I and auto * p = & I; // p is the pointer to the integer constant I const auto j2 = I, & k2 = I; // j2 is an integer constant, k2 is the constant reference of int I


 

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.