1.Understand auto type deduction
①auto自动类型推导和模板的类型推导几乎一样,只在少数地方有差别
②当一个变量用auto声明的时候
template<typename T>
void f(ParamType param);
auto所扮演的角色 就是T, 类型修饰符扮演的角色 就是param
③条款1 对auto仍然适用(我前面的博客写过)
• Case 1: The type specifier is a pointer or reference,
but not a universal reference.
• Case 2: The type specifier is a universal reference.
Case 3: The type specifier is neither a pointer nor a reference.
2. 一些样例(这些前面都提到过)
auto&& uref1 = x; // x is int and lvalue,
// so uref1's type is int&
auto&& uref2 = cx; // cx is const int and lvalue,
// so uref2's type is const int&
auto&& uref3 = 27; // 27 is int and rvalue,
// so uref3's type is int&&
const char name[] = // name's type is const char[13]
"R. N. Briggs";
auto arr1 = name; // arr1's type is const char*
auto& arr2 = name; // arr2's type is
// const char (&)[13]
void someFunc(int, double); // someFunc is a function;
// type is void(int, double)
auto func1 = someFunc; // func1's type is
// void (*)(int, double)
auto& func2 = someFunc; // func2's type is
// void (&)(int, double)
3.关键来了 auto和模板类型推导的区别
①当你要定义一个int类型的变量的时候,你可以这么干
上述4个都是固定类型 fixed types
C++98
int x1 = 27;
int x2(27);
用C++支持的一致性初始化
int x3 = { 27 };
int x4{ 27 }
四种定义的方式,最后结果都是相同的,一个int型变量被初始化成27
②换成auto呢?
auto x1 = 27;
auto x2(27);
auto x3 = { 27 };
auto x4{ 27 };
结果如下
auto x1 = 27; // type is int, value is 27
auto x2(27); // ditto
auto x3 = { 27 }; // type is std::initializer_list<int>,
// value is { 27 }
auto x4{ 27 }; // ditto
std::initializer_list类型对象是一个访问const T类型对象数组的轻量代理对象
auto推断出来的结果不一样,这就是差别
auto定义的变量 后面用大括号初始化的时候 会被推断成 std::initializer_list
③大家要注意 此处有两种类型推断发生
1.是推断成std::initializer_list< T >
2.是推断成std::initializer_list< int >
4.一些注意点
①以下代码是错误的
auto x = { 11, 23, 9 }; // x's type is
// std::initializer_list<int>
template<typename T> // template with parameter
void f(T param); // declaration equivalent to
// x's declaration
f({ 11, 23, 9 }); // error! can't deduce type for T
②正确做法
template<typename T>
void f(std::initializer_list<T> initList);
f({ 11, 23, 9 }); // T deduced as int, and initList's
// type is std::initializer_list<int>
③C++14 还允许auto来推断返回值 和 用于lambda表达式的形参声明,但他们都是用的是auto的模板类型推断, 所以
auto createInitList()
{
return { 1, 2, 3 }; // error: can't deduce type
} // for { 1, 2, 3 }
std::vector<int> v;
…
auto resetV =
[&v](const auto& newValue) { v = newValue; }; // C++14
…
resetV({ 1, 2, 3 }); // error! can't deduce type
// for { 1, 2, 3 }