"C++11 new feature" Auto keyword __c++

Source: Internet
Author: User

Original works, reprint please indicate: http://blog.csdn.net/Xiejingfa/article/details/50469045

As anyone familiar with the scripting language knows, many scripting languages introduce "type auto inference" techniques: For example Python, you can declare variables directly, and type check at runtime. With the release of the C++11 standard, the C + + language also introduces the function of type automatic inference, which is the Auto keyword we are going to introduce today.

C + + is a strongly typed language, and you must explicitly indicate its type when declaring a variable. However, in practice, it is difficult to infer the type of the value of an expression, especially with the appearance of the template type, which makes it more difficult to understand the return type of some complex expressions. To solve this problem, c++11 redefined the semantics of the Auto keyword for automatic type inference. Why do you say "redefine"? The Auto keyword has appeared in c++98 for the declaration of automatic variables, but is rarely used (see here for this definition). usage

C + + introduction of the AUTO keyword has two main uses: one is to automatically infer the type of the variable based on the initialization expression when the variable is declared, and the second is the placeholder for the return value of the function when the function is declared. For automatic type inference, C++11 also has a similar keyword decltype, interested in child shoes can continue to pay attention to my blog, today we mainly to explain some of the use of the Auto keyword. Automatic type inference

Using auto to infer the data type of a variable from an initialization expression can greatly simplify our programming efforts, especially for some types of lengthy and complex variables. For example, in the previous practice, for vector-type variables, if we needed to get its iterator, we would need to declare vector::iterator ITER, and after using the Auto keyword we could have the compiler help us infer the exact type of the iterator. In addition, when the template function is defined, if the type of the variable depends on the template parameters, it is also difficult to determine the type of the variable, the use of the Auto keyword can be the "dirty live dirty" to the compiler to complete.

Let's look at some simple examples:

 #include <iostream> #include <vector> using namespace std;
    Template<class T, class u> void Add (T T, u u) {Auto S = t + u;

cout << "type of T + U is" << typeid (s). Name () << Endl;
    int main () {//simple automatic type inference auto a = 123;
    cout << "Type A is" << typeid (a). Name () << Endl;
    Auto S ("Fred");

    cout << "type of S is" << typeid (s). Name () << Endl;
    Verbose type descriptions (such as iterators) vector<int> VEC;
    Auto iter = Vec.begin ();

    cout << "Type of ITER is" << typeid (ITER). Name () << Endl;
When using template technology, if the type of a variable depends on the template parameter, use Auto to determine the variable type Add (101, 1.1); }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18-------------19 20 21 22 23 24 25 26 27-1 2 3 4, 5 21 22 23 24 25 26 27 function return-value placeholder

In this case, auto is primarily used with the Decltype keyword as a placeholder for returning a value type. At this point, the keyword does not represent automatic type detection, only as part of the syntax that represents the return value of the post.

Examples are as follows:

Template<class T, class u>
Auto Add (T T, u u)-> decltype (t + u) 
{return
    t + u;
}
1 2 3 4 5 1 2 3 4 5 Attention Matters

1. Variables using the Auto keyword must have an initial value.

2, you can use the valatile,* (pointer type descriptor),& (reference type descriptor),&& (right value reference) to decorate the Auto keyword.

    Auto A = ten;
    Auto *PA = new Auto (a);
    Auto **rpa = new Auto (&a);
    cout << typeID (a). Name () << Endl;   Output: int
    cout << typeid (PA). Name () << Endl;  Output: int *
    cout << typeid (RPA). Name () << Endl;//output: int * *
1 2 3 4 5 6 1 2 3 4 5-6

3, function parameters and template parameters can not be declared as auto.

4, use the AUTO keyword to declare the type of the variable, can not automatically deduce the top level of cv-qualifiers and reference type, unless the display declaration. For example, when using the Auto keyword for type derivation, if the initialization expression is a reference type, the compiler removes the reference unless the declaration is displayed. For example:

    int i = ten;
    int &r = i;
    Auto A = R;
    A = 13; Re-assign
    cout << "i =" << i << "a =" << a << Endl;    Output i=10,a=13

    //Explicit declaration of
    Auto &b = r;
    b = 15; Re-assign
    cout << "i =" << i << "b =" << b << Endl;    Output i=15,a=15
1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10 Using auto keyword for type derivation, the compiler automatically ignores the top-level const unless a declaration is displayed. For example:
    const int C1 = Ten;
    Auto C2 = C1;
    C1 = 11; Error, C1 is const int type, cannot modify const variable
    c2 = 14;//correct, c2 for int type

    //Display declaration
    Const Auto C3 = C1;
    C3 = 15; Error, C3 is const int type, const variable cannot be modified
1 2 3 4 5 6 7 8 1 2 3 4 5 6-7 8

5. For array types, the Auto keyword is deduced to be a pointer type, unless it is declared as a reference. For example:

    int a[10];
    Auto B = A;
    cout << typeid (b). Name () << Endl;   Output: int *

    Auto &c = A;
    cout << typeID (c). Name () << Endl;   Output: int [10]
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.