Today learned a little keyword, inadvertently found himself has never been exposed to the auto
Okay, I'm starting to crap again!
Automatic storage duration. (deprecated)
1) When declaring variables in block scope, in namespace scope, in init statements of for loops, etc, the type of the Vari Able may omitted and the keyword auto is used instead. Once the type of the initializer have been determined, the compiler determines the type that would replace the keyword auto As if using the rules for template argument deduction from a function call. The keyword Auto May is accompanied by modifies, such as const or & which'll participate in the type deduction. For example, given const auto& i = expr;, the type if I was exactly the type of the argument u in an imaginary template Template<class u> void F (const u& U) If the function call F (expr) is compiled.
2) in a function declaration, the keyword auto does not perform automatic type detection. It is serves as a part of the trailing return type
Syntax. (never found this)
1. The general meaning explains how auto is used (except for functions),
2. (University four year four not over, this year I do not test) general meaning (in a function declaration, the Auto keyword cannot be used as a type returned by a function, it can only be used as part of the return detail symbol ...). )
1#include <iostream>2#include <cmath>3#include <typeinfo>4template<classTclassU>5Auto Add (T t,u U)->decltype (t+u)//a part of a symbol so that understanding is not misplaced. 6 {7 returnt+u;8 }9 //get a pervert.TenAuto Get_fun (intARGC)Double(*) (Double)//returns a function that doubles x (double) because it is to address, so use * One { A Switch(argc) { - Case 1: - returnStd::fabs; the Case 2: - returnStd::sin; - default: - returnStd::cos; + } - } + intMain () A { atAuto A=add (1,2.54); -Std::cout<<typeid (a). Name () <<Std::endl; -Auto B=add ('1','A'); -Std::cout<<typeid (b). Name () <<Std::endl; - -Auto P=get_fun (2); inStd::cout<<p (3.14) <<std::endl;//cos (3.14) - } to //Output + /** - d----------->double the I------------>int (converted to int) * 0.00159265---------->==0 $ */
This article references: http://tool.oschina.net/apidocs/apidoc?api=cpp%2Fen%2Fcpp.html (Auto)
There's a way to use auto