C++11 is an extension and correction of the current C + + language. C++11 contains a number of new features: Contains lambda expressions, type derivation Keywordauto, Decltype, and a number of improvements to templates.
g++ compile c++11 command plus
-std=c++11
The first function of introducing auto in C++11 is to derive the active type.
Auto's own active type deduction, which is used to determine the data type of a variable from an initialization expression.
By auto's own active type deduction, we can simplify our programming work
Auto actually compiles the variables at compile-time, so it does not adversely affect the execution efficiency of the program. In addition, it seems that auto does not affect the compilation speed, because the compile time is also the right side deduction and then infer whether the left side matches.
#include <iostream> #include <typeinfo> #include <vector>using namespace Std;template<class T, Class u>void func (T T, u u) {auto ret = t*u;} int main () {//auto A; Error, Auto is a row type deduction through an initialization expression, assuming there is no initialization expression, there is no way to determine the type of a auto i = 1;cout << typeid (i). Name () << Endl; Output is i,g++ compiler output i is int type, MS's vs2008 output is intauto d = 1.0;cout << typeid (d). Name () << Endl; Output is d,double type auto str = "Hello c++11"; cout << str << endl;vector<int> iv;iv.push_back (1); iv.push_ Back (2); Iv.push_back (3), for (auto Itr=iv.begin (); Itr!=iv.end (); itr++)//self-derivation for iterators cout << *itr << Endl; return 0;}
Other than that. When using the template technology. Assuming that the type of a variable depends on the template parameters, it is very difficult to determine the type of the variable (after using auto, it will be determined by the compiler itself).
The following is a detailed example.
Template <class T, class u>void Multiply (T T, u u) {Auto V = t*u;}
The Autokeyword of new characteristics of c++11