[+] There are two main uses of auto introduced in c++11: Automatic type inference and the return value placeholder. The semantics of the temporary variable that the auto is identifying in c++98 has been removed in c++11 because it is rarely used and is redundant. Before and after the two standard auto, is completely two concepts.
1. Automatic type inferenceAuto Auto type inference, which is used to infer the data type of a variable from an initialization expression. Auto type inference can greatly simplify our programming work. Here are some examples of using auto.
[CPP] View plain copy print? #include <vector> #include <map> using namespace std; Int main (int argc, char *argv[], char *env[]) { // auto a; // error, no initialization expression, cannot infer the type of a ( // auto) int a = 10; // error, Auto temp variable semantics in C + + , is no longer present in 11. This is the use of the old standard. // 1. Auto Help derivation type auto a = 10; auto c = ' a '; auto s ("Hello"); // 2. type verbose &NBSP;&NBSP;&NBSP;&NBSP;MAP<INT,&NBSP;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, // is difficult to determine the type of the variable without using auto (auto is determined by the compiler). template <class t, class u> void multiply (T t, u u) { auto v = t * u; } 2. The return value occupies a position
[CPP] view plain copy print? 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. Use precautions ① we can use Valatile,pointer (*), reference (&), rvalue Reference (&&) to decorate auto
[CPP] view plain copy print? Auto k = 5; auto* PK = new auto (k);