理解自动类型推导 auto

本文探讨了C++中auto关键字的使用及其类型推导机制,对比了auto与模板类型推导的不同之处,并通过实例说明了如何正确使用auto以及初始化列表。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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++14resetV({ 1, 2, 3 }); // error! can't deduce type
 // for { 1, 2, 3 }

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值