C++11 new features: Automatic type inference and type acquisition

Source: Internet
Author: User

Disclaimer: This article was written on the basis of Alex Allain's article http://www.cprogramming.com/c++11/c++11-auto-decltype-return-value-after-function.html.

Added a lot of personal understanding, not translation.

Reprint please specify the source http://blog.csdn.net/srzhz/article/details/7934483

Automatic type inference

When the compiler can infer its type in the declaration of a variable, you can use the auto keyword as their type:

[CPP]View Plaincopy
    1. Auto x = 1;


The compiler certainly knows that x is of type integer. So you don't have to use Int. People who have been exposed to generic programming or API programming can probably guess what automatic type inference is for: saving you a lot of lengthy type declaration statements.

For example, the following:

In the original C + +, you would want to use the vector iterator to write this:

[CPP]View Plaincopy
    1. vector<int> Vec;
    2. vector<Int>::iterator ITR = Vec.iterator ();

It looks very uncomfortable. Now you can write it like this:

[CPP]View Plaincopy
    1. vector<int> Vec;
    2. Auto ITR = Vec.iterator ();

It's more decisive and concise. It would be too naive to say that automatic type inference is the only way to use it. In many cases it can also provide a deeper level of convenience.

For example, there is this code:

[CPP]View Plaincopy
    1. Template <TypeName Builttype, TypeName builder>
    2. void
    3. Makeandprocessobject (const builder& Builder)
    4. {
    5. Builttype val = Builder.makeobject ();
    6. //Do stuff with Val
    7. }

The function is to use the instance generated by the builder's makeobject to do certain things. However, generic programming is now introduced. The type of builder is different, then the type of Makeobject return is different, so we have to introduce two generics here. It looks complicated, so you can use automatic type inference to simplify operations here:

[CPP]View Plaincopy
    1. Template <TypeName builder>
    2. void
    3. Makeandprocessobject (const builder& Builder)
    4. {
    5. Auto val = Builder.makeobject ();
    6. //Do stuff with Val
    7. }


Because after the type of builder, the compiler can already know the return value type of Makeobject. So we can get the compiler to infer the Val type automatically. This eliminates a generic type.

Do you think automatic type inference is the only way to use this? That's too naive. C++11 also allows type inference for the return value of a function

New return value syntax and type get (Decltype) statements in the original, we declare a function like this: [CPP]View Plaincopy
    1. int temp (int A, double b);

The preceding int is the return value type of the function, temp is the function name, int A, and double b is the argument list.

You can now define the function return value type after it has been moved to the parameter list:

[CPP]View Plaincopy
    1. Auto Temp (int A, double b), int;


A post-return value type can be useful. For example, the following class definitions are available:

[CPP]View Plaincopy
    1. Class Person
    2. {
    3. Public
    4. enum PersonType {adult, child, SENIOR};
    5. void Setpersontype (PersonType person_type);
    6. PersonType Getpersontype ();
    7. Private
    8. PersonType _person_type;
    9. };


So when we define the Getpersontype function we have to write this:

[CPP]View Plaincopy
    1. Person::P Ersontype person::getpersontype ()
    2. {
    3. return _person_type;
    4. }


Because the class in which the function is located is declared after the function return value, the compiler does not know which class the function is in when writing the return value. Because PersonType is an enumeration of the internal declarations of the person class, the compiler cannot find this type when you see PersonType. So you have to add the person in front of the PersonType::, tell the compiler that this type belongs to person. This looks like a bit of a problem. When you use the new return value syntax, you can write this:

[CPP]View Plaincopy
    1. Auto Person::getpersontype ()-PersonType
    2. {
    3. return _person_type;
    4. }


Because this time the compiler sees the return value type PersonType, it already knows that this function belongs to the class person. So it goes to the person class to find the enumeration type.

Of course, the above application can only be said to be a artifice. It didn't help us much (the code didn't even get shorter). So we have to introduce another function of c++11.

Type Acquisition (DECLTYPE) since the compiler is able to infer the type of a variable, we should be able to display the type information for a variable in the code. So C + + introduces another feature: Decltype. (I really don't know how to translate this word, let's call it type acquisition). [CPP]View Plaincopy
    1. int x = 3;
    2. Decltype (x) y = x;  //same thing as auto y = x;

The above code uses the type fetching function. Combined with the back-to-back syntax of a function return value, you can apply the following: [CPP]View Plaincopy
    1. Template <TypeName builder>
    2. Auto
    3. Makeandprocessobject (const builder& Builder), Decltype (Builder.makeobject ())
    4. {
    5. Auto val = Builder.makeobject ();
    6. //Do stuff with Val
    7. return Val;
    8. }

In the previous example, the return value of this function is void, so there is no need to introduce generics for the return value. If the return value is the return value of makeobject, then the function has to introduce two generics. Now that we have the type fetch function, we can automatically infer the Makeobject type in the return value. So Decltype did provide us with a lot of convenience. This is a very important feature, and in many cases, especially when you introduce generic programming, you may not remember the type or type of a variable that is too complex to write. You will have the flexibility to use Decltype to get the type of the variable.

The role of automatic type inference in lambda expressions (anonymous functions)

Please refer to http://blog.csdn.net/srzhz/article/details/7934652

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.