New C + + features-auto keywords

Source: Internet
Author: User

1 Auto keyword
Before we introduce, let's start by understanding a new keyword auto.
In C++11, if the compiler can infer the type of a variable when it is declared, then instead of having to put the variable type before the declaration, you can declare the variable directly with auto:

int x = 4;

In other words, the above declaration statement can be replaced by the following statement:

auto x = 4;

Of course, this is certainly not the fundamental purpose of the Auto keyword design. Its most important function is to work with templates and containers:

vector<int> vec;auto// 代替vector<int>::iterator itr

In other places, auto can also be purchased in a handy place. For example, the following example:

template <typenametypename Builder>void makeAndProcessObject (const Builder& builder){    BuiltType val = builder.makeObject();    // do stuff with val}

In this code, there are two required template parameters, one as the object ' Builder ' type, and the other as the type of object being constructed. To make it worse, the type of the building object cannot be inferred from the template parameters. Each invocation must resemble:

MyObjBuilder builder;makeAndProcessObject<MyObj>( builder );

However, auto can reduce this ugliness because you don't need to write the specified type when you build the object. And all this, C + + is done for you, just like this:

template <typename Builder>void makeAndProcessObject (const Builder& builder){    auto val = builder.makeObject();    // do stuff with val}

Now, you just need a simple template parameter, and when you call this function, another parameter can be very simply inferred:

MyObjBuilder builder;makeAndProcessObject( builder );

For callers, this is a better approach, and the template code has nothing to lose in terms of readability, and if so, it's easier to understand.
2 Decltype and the fun of the new return grammar
Well, here you might ask, is very powerful, but I want to return the value created by the Builder object what to do? The experts of the standard Committee, of course, also thought of the problem, and they provided ingenious solutions: type-fetching function Decltype () and the new return syntax.

int multiply (intint y);

C++11: The return value is placed at the end of the function declaration, and the replacement return value type is the Auto keyword, as follows:

auto multiply (intintint;

So would you want? Let's look at a simple example where it helps Us:a class with an enum declared inside it:

class Person{public:    enum PersonType { ADULT, CHILD, SENIOR };    void setPersonType (PersonType person_type);    PersonType getPersonType ();private:    PersonType _person_type;};

Here, we have a simple class, person, and the type of the enum type data. But what happens when you define the method of this class?
The first class method, no problem, you can use the PersonType enumeration type:

void Person::setPersonType (PersonType person_type){    _person_type = person_type;}

The second method is a bit confusing. This seemingly clean code is not compiled:

// 编译器不知道PersonType是什么?因为其在Person类外面PersonType Person::getPersonType (){    return _person_type;}

To compile through, you have to write this:

Person::PersonType Person::getPersonType (){    return _person_type;}

The above code enables the function to work correctly. But it's very easy to make mistakes, especially when it comes to introducing templates that become more confusing.
At this point, we need to introduce a new return syntax. Put the return value at the end of the function, you do not need to add the scope of the class action. When the compiler reaches the return value, it already knows that the function is part of the person class, so you know what type PersonType is.

Person() -> PersonType{    return _person_type;}

Well, even if it's beautiful, can it really help us solve the problems? Before we had no new syntax, we didn't solve the problem. It's not over yet! Let's introduce a new concept: Decltype.
3 Decltype
Decltype is not the demon oviparous brother of Auto. Auto allows us to declare a variable with a specific type; Decltype Let's extract the type from a variable or expression. What do you mean? See below:

int3;decltype// same thing as auto y = x;

Now that you can act on an expression, here's the following:

decltype( builder.makeObject() )

That gives us the type, isn't it? In conjunction with the new syntax, we re-implement the template introduced at the beginning:

template <typename Builder>auto makeAndProcessObject (constdecltype( builder.makeObject() ){    auto val = builder.makeObject();    // do stuff with val    return val;}

Unfortunately, this can only be used in conjunction with the new syntax. The old return syntax is not available for the builder return type.
4 Auto, References, pointers and Const
Use of Auto and reference &:

int//intorint?

C++11 has a brief introduction: Auto defaults to the return value of a reference. So, in the code above, it's int. But you can explicitly add &, forcing to become a reference:

int& foo();auto// intauto// int&

Auto pointer:

int* foo();auto// int*

You can also explicitly indicate the pointer as follows:

int* foo();auto// int*

Use of auto and const, combined with references:

int& foo();constauto// const int&

Or, with the pointer

int* foo();constint* const_foo();constauto// const int*auto// const int*

In general, it looks like the normal C + + template type interface rules, doesn't it?
1.5 is the compiler supported?
The new features mentioned in this article are supported by GCC 4.4 and MSVC 10. Of course, at compile time, you need to specify the standard, in GCC with-std=c++0x.
If you are using a different compiler, you need to see how it supports c++11.

New C + + features-auto keywords

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.