First, auto type deduction is exactly the same as template type deduction . The only difference is that when deducing the expression enclosed in curly braces, auto will deduct it into a type of std::initializer_list. The code example is as follows:
auto x1 = 27; //x1型别为int
auto x2(27); //x2型别为int
auto x3 = {27}; //x3型别为std::initializer_list<int>
auto x4{27}; //x4型别为std::initializer_list<int>
auto x5 = {27,13,0.5}; //x5型别无法推导,编译报错,因为里面包含两种数据类型
Shorthand
- The auto type deduction is exactly the same as the template type deduction . The only difference is that when the expression enclosed in curly braces is deduced, auto deduces it to a type of std::initializer_list.
- Using auto in the return value of a function or lambda expression will use template deduction instead of auto deduction