C + + 11 Learning 1: Type auto-derivation auto and Decltype

Source: Internet
Author: User

Cocos 3.x uses a lot of C + + 11 stuff, so as a loyal C + + fan, it is necessary to do a systematic study of C + + 11.

Before using c++11, be sure to be aware of the compiler's support for c++11, and some compilers are not fully supported, such as the vs2012 I'm using. This is the support for C + + 11 for the c++11 feature in MSDN vs editions (modern C + +)

1.auto

Auto this keyword C + + was originally used to specify the memory. Because very few people to use this thing, so in the c++11 of the original auto function to discarded, and become the type of the derivation of the key word now. First, the simple use of auto:

#include <iostream>#include<vector>#include<map>using namespacestd;intMain () {Auto num=1;//num is of type intAuto C ='A';//c is a char typeAuto Str ("Test");//Str is a char arrayMap<string, vector<int>>map;  for(Auto it = begin (map); it! = end (map); ++it)//It is an iterator type    {    }    return 0;}

The most obvious advantage of this is that when using iterators to traverse STL containers, it is not necessary to declare the types of those iterators, nor to use a typedef to implement a very concise implementation traversal.

In terms of efficiency, auto does not have an impact on run time efficiency, which is derived at compile time, such as the above code, which converts the NUM variable to the int type when compiling.

There are notable differences between auto and other variable types:

The variable declared by 1.auto must be initialized, or the compiler cannot determine the type of the variable.

2.auto cannot be declared as a return value, auto cannot be a formal parameter, auto cannot be decorated as a template parameter

3.

2.decltype

The

decltype keyword and auto correspond to each other, and they are often used in conjunction with some places. Decltype can determine the type of a variable or expression at compile time, for example:

 #include <iostream> #include  <vector>  #include  <map>using  namespace   Std;  void   func (auto can) { int   A;}  int    Main () {auto num  = 1 ; // num is the int type  decltype (num) num2 = num; //    num2 is also of type int  return  0  ;}  

Here Decltype gets the num type, and then uses this type to define NUM2 to do a copy of Num. Auto and Decltype also have a classic use case, see the following example:

Template <typename T1, typename t2>  -decltype (t1 + T2)  {     return t1+ T2;  }

Here is a template to calculate the addition of two variables, if the two types T1 and T2 different words, T1+T2 type will be determined by the compiler, so that the use of decltype can get the type of return. However, if Decltype (T1 + T2) is placed in front of the function name as the return value, according to the compiler's parsing order, when parsing to the return value Decltype (t1 + t2) T1 and T2 is not defined, so to re-declare the type of T1 and T2, It can be complicated and difficult to read. This changes the grammar rules, puts decltype (t1 + T2) behind the function, and uses auto as the return value to tell the compiler that the true return value is after the function declaration. In a nutshell, auto can be used as a return value placeholder to get the return value back in place.

C + + 11 Learning 1: Type auto-derivation auto and Decltype

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.