This post consists of: http://www.sollyu.com/c11-new-features-auto/
Article List
This article is a series of articles
The use of the "c++11" new feature--auto http://www.sollyu.com/c11-new-features-auto/
"C++11" new feature--lambda function http://www.sollyu.com/c11-new-lambda-function/
Description
The auto introduced in C++11 has two main purposes: 自动类型推断
and 返回值占位
.
The semantics of Auto's identity in c++98 临时变量
has been removed in c++11 because of its minimal use and redundancy. Before and after two standard auto, is completely two concepts.
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 namespace STD;int Main(int argc, Char *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 type Auto a = Ten; Auto C = ' A '; Auto s("Hello"); //2. Lengthy type Map<int, Map<int,int> > MAP_; Map<int, Map<int,int>>::const_iterator ITR1 = MAP_.begin(); Const Auto 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,//Do not use auto will be difficult to determine the type of variable (after using auto, will be determined automatically by the compiler). Template <class T, class U>void Multiply(T T, U u){?? ?Auto v = T * u;}
return value placeholder
Template <TypeName T1, TypeName T2>Auto Compose(T1 T1, T2 T2) - Decltype(T1 + T2){ return T1+T2;}Auto v = Compose(2, 3.14); // v' s type is double
Precautions for use
① we can use,,, valatile
pointer(*)
reference(&)
rvalue reference(&&)
to decorate auto
Auto k = 5;Auto* PK = New Auto(k);Auto** PpK = New Auto(&k);Const Auto N = 6;
② a variable declared with auto must be initialized
Autom; //m should be intialized
③auto cannot be combined with other types of combinations
Autointp; // This is The practice of old auto.
④ function and template parameters cannot be declared as auto
void MyFunction(Auto parameter){} // No Auto as Method argumentTemplate<Auto T> // Utter nonsense - not allowedvoid Fun(T T){}
⑤ a variable defined on a heap, an expression that uses auto must be initialized
int* P = New Auto(0); //Fineint* pp = New Auto(); // should be initializedAuto x = New Auto(); // Hmmm ... No IntializerAuto* y = New Auto(9); // Fine. Here y is a int*Auto Z = New Auto(9); //Fine. Here Z is a int* (It is not just an int)
⑥ that 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 value = 123;Auto X2 = (Auto)value; // No Casting using AutoAuto X3 = static_cast<Auto>(value); // same as above
⑦ a variable defined in an auto sequence must always be deduced into the same type
Autox1= 5,x2 = 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 = About;Auto J = I; // J is int, rather than Const intJ = - // Fine. as J is not constant// Now Let US Try to have Referenceauto& k = I; // Now k is Const int&k = -; // Error. k is constant// similarly with volatile Qualifer
⑨auto will degenerate exponentially pointers to arrays, unless declared as references
int a[9];Auto J = a;cout<<typeID(J).name()<<Endl; // This would Print int*auto& k = a;cout<<typeID(k).name()<<Endl; // This would Print int [9]
The use of the new feature--auto "C++11"