Transfer from http://blog.csdn.net/huang_xw/article/details/8760403
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. Before and after two standard auto, is 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.
[CPP]View Plaincopyprint?
- #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 = 10;
- 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,
- It is difficult to determine the type of a variable without using auto (the compiler will automatically determine it after using auto).
- Template <class T, class u>
- void Multiply (T T, u u)
- {
- Auto V = t * u;
- }
2. Return-Value Placeholder
[CPP]View Plaincopyprint?
- 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
3. Precautions for use
① we can use Valatile,pointer (*), reference (&), rvalue Reference (&&) to decorate auto
[CPP]View Plaincopyprint?
- 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
[CPP]View Plaincopyprint?
- Auto m; //M should be intialized
③auto cannot be combined with other types of combinations
[CPP]View Plaincopyprint?
- Auto int p; //This is the practice of old auto.
④ function and template parameters cannot be declared as auto
[CPP]View Plaincopyprint?
- void MyFunction (auto parameter) {} //No auto as method argument
- Template<auto t> //utter nonsense-not allowed
- void Fun (T-t) {}
⑤ a variable defined on a heap, an expression that uses auto must be initialized
[CPP]View Plaincopyprint?
- int* p = New auto (0); //fine
- int* pp = new auto (); //should be initialized
- Auto x = new Auto (); //hmmm ... no intializer
- auto* y = new auto (9); //Fine. Here y is a int*
- Auto z = New auto (9); //fine. Here Z is a int* (It isn't 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
[CPP]View Plaincopyprint?
- int value = 123;
- Auto x2 = (auto) value; //No casting using auto
- Auto x3 = static_cast<auto> (value); //Same as above
⑦ a variable defined in an auto sequence must always be deduced into the same type
[CPP]View Plaincopyprint?
- Auto X1 = 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
[CPP]View Plaincopyprint?
- const INT i = 99;
- Auto J = i; //j is int, rather than const int
- j = + //Fine. As J is not constant
- Now let us try to have 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
[CPP]View Plaincopyprint?
- int a[9];
- Auto J = A;
- cout<<typeID (j). Name () <<endl; //This'll print int*
- auto& k = A;
- cout<<typeid (k). Name () <<endl; //This would print int [9]
The use of the new feature--auto "C++11"