The auto introduced in C++11 has two main purposes: automatic type inference and return-value placeholder. The semantics of the identity temp variable in auto in C++98, which has been removed in c++11 because of its minimal use and redundancy. Front and back two standard auto, completely two concepts
1. Automatic type inference
Auto automatic type inference, which is used to infer the data type of a variable from an initialization expression. Automatic type inference from auto can greatly simplify our programming efforts. Here are some examples of using auto.
#include <vector>#include<map>using namespacestd; intMainintargcChar*argv[],Char*env[]) { //Auto A; //error, no initialization expression, cannot infer type of a//Auto int a = 10; //error, the semantics of the auto temp variable no longer exist in c++11, which is the use of the old standard. //1. Automatic Help deduction typeAuto A =Ten; Auto C='A'; Auto S ("Hello"); //2. Lengthy typemap<int, map<int,int> >MAP_; Map<int, map<int,int>>::const_iterator ITR1 =Map_.begin (); ConstAuto ITR2 =Map_.begin (); Auto PTR=[] () {std::cout<<"Hello World"<<Std::endl; }; return 0; }; //3. When using template technology, if the type of a variable depends on the template parameter,//It is difficult to determine the type of a variable without using auto (the compiler will automatically determine it after using auto). Template <classTclassU>voidMultiply (T T, u u) {Auto V= T *u; }
2. Return placeholders:
Template <typename T1, typename t2> -decltype (t1 + T2) { return t1+ T2; } = Compose (23.14//
3. Precautions for use:
① we can use Valatile,pointer (*), reference (&), rvalue Reference (&&) to decorate auto
5 ; Autonew auto (k); Autonew Auto (&k); Const 6;
② variables declared with auto must be initialized
//
③auto cannot be combined with other types of combinations
int // This is the practice of old auto.
④ function and template parameters cannot be declared as auto
void // no auto as method argument Template// utter nonsense-not allowed void fun (t t) {}
⑤ a variable defined on a heap , an expression that uses auto must be initialized
int* p =NewAuto0);//Fineint* pp =NewAuto ();//should be initializedAuto x=NewAuto ();//hmmm ... no intializerAuto* y =NewAuto9);//Fine. Here y is a int*Auto Z =NewAuto9);//Fine. Here Z is a int* (It isn't just an int)
⑥Auto is a placeholder, not a type of his own , and therefore cannot be used for type conversions or other operations such as sizeof and typeid.
int 123 ; // no casting using auto // Same as above
⑦ a variable defined in an auto sequence must always be deduced into the same type
5 5.0, x3='r'; // This is too much....we cannot combine
⑧auto cannot be automatically deduced as Cv-qualifiers (constant & volatile qualifiers) unless declared as a reference type
const int i = 99 ; Auto J = i; // j is int, rather than const int j = 100 // Fine. As J isn't constant // Now let us try to hav E reference auto& k = i; // now K is const int& k = 100 ; // Error. K is constant // Similarly with volatile qualifer
⑨auto will degenerate exponentially pointers to arrays, unless declared as references
int a[9]; = A; cout// This would print int* Auto& k = A; cout// this would print int [9]
"Turn from" http://blog.csdn.net/huang_xw/article/details/8760403
Three--auto of new characteristics of c++11