条款二 了解auto类型推断

基础知识

除了一处例外,auto的类型推断与template一样。存在一个直接的从template类型推断到auto类型推断的映射

三类情况下的推断如下所示:

// case 1
const auto& rx = x; // rx -> int // case 2
auto&& uref1 = x; // uref1 -> int&
auto&& uref2 = cx; // uref2 -> const int&
auto&& uref3 = ; // uref3 -> int&& // case 3
auto x = ; // x -> int
const auto cx = x; // x -> int

对于数组和函数,有如下推断:

const char name[] = "R. N. Briggs";
auto arr1 = name; // arr1 -> const char*
auto& arr2 = name; // arr2 -> const char(&)[13] void someFunc(int, double);
auto func1 = someFunc; // func1 -> void (*)(int, double)
auto& func2 = someFunc; // func2 -> void (&)(int, double)

但是在使用初始化列表(std::initializer_list)的情况下,auto的类型推断会有问题

auto x = ; // x1 -> int
auto x2(); // x2 -> int
auto x3 = {}; // x3 -> std::initializer_list<int>
auto x4{}; // x4 -> std::initializer_list<int>

列表初始化是auto与template类型推断的唯一区别,template函数推断中不能使用列表形式的初始化{}

auto x = {, , };
template<typename T>
void f(T param);
f({, , }); // wrong! template<typename T>
void f(std::initializer_list<T> initList);
f({, , }); // T -> int, initList -> std::initializer_list<int>

C+14可以使用auto来推断函数返回类型,并且lambda可以在形参声明中使用auto,但是此处的auto使用template类型推断而非auto类型推断,所以返回列表初始化将报错

auto createInitList() {
return {, , };
} // error! std::vector<int> v;
auto resetV = [&v](const auto& newValue) {v = newValue};
resetV({, , }) // error!

总结

  • auto类型推断总是和template类型推断一样,但是auto类型推断把列表初始化解析成std::initializer_list,template类型推断则不然
  • 在函数返回类型以及lambda形参中的auto,采用template类型推断而非auto类型推断

[Effective Modern C++] Item 2. Understand auto type deduction - 了解auto类型推断的更多相关文章

  1. [Effective Modern C++] Item 1. Understand template type deduction - 了解模板类型推断

    条款一 了解模板类型推断 基本情况 首先定义函数模板和函数调用的形式如下,在编译期间,编译器推断T和ParamType的类型,两者基本不相同,因为ParamType常常包含const.引用等修饰符 t ...

  2. [Effective Modern C++] Item 3. Understand decltype - 了解decltype

    条款三 了解decltype 基础知识 提供一个变量或者表达式,decltype会返回其类型,但是返回的内容会使人感到奇怪. 以下是一些简单的推断类型: ; // decltype(i) -> ...

  3. [Effective Modern C++] Item 5. Prefer auto to explicit type declarations - 相对显式类型声明,更倾向使用auto

    条款5 相对显式类型声明,更倾向使用auto 基础知识 auto能大大方便变量的定义,可以表示仅由编译器知道的类型. template<typename It> void dwim(It ...

  4. [Effective Modern C++] Item 6. Use the explicitly typed initializer idiom when auto deduces undesired types - 当推断意外类型时使用显式的类型初始化语句

    条款6 当推断意外类型时使用显式的类型初始化语句 基础知识 当使用std::vector<bool>的时候,类型推断会出现问题: std::vector<bool> featu ...

  5. [Effective Modern C++] Item 4. Know how to view deduced types - 知道如何看待推断出的类型

    条款四 知道如何看待推断出的类型 基础知识 有三种方式可以知道类型推断的结果: IDE编辑器 编译器诊断 运行时输出 使用typeid()以及std::type_info::name可以获取变量的类型 ...

  6. [Effective Modern C++] Item 7. Distinguish between () and {} when creating objects - 辨别使用()与{}创建对象的差别

    条款7 辨别使用()与{}创建对象的差别 基础知识 目前已知有如下的初始化方式: ); ; }; }; // the same as above 在以“=”初始化的过程中没有调用赋值运算,如下例所示: ...

  7. Effective Modern C++ Item 27:重载universal references

    假设有一个接收universal references的模板函数foo,定义如下: template<typename T> void foo(T&& t) { cout ...

  8. Effective Modern C++ Item 37:确保std::thread在销毁时是unjoinable的

    下面这段代码,如果调用func,按照C++的标准,程序会被终止(std::terminate) void func() { std::thread t([] { std::chrono::micros ...

  9. Effective Modern C++ 42 Specific Ways to Improve Your Use of C++11 and C++14

    Item 1: Understand template type deduction. Item 2: Understand auto type deduction. Item 3: Understa ...

随机推荐

  1. JS截取字符串:slice(),substring()和substr()

    var string='abcdefg' 1.slice() string.slice(startLocation [, endLocation]) ps1:2个参数可以为负数,若参数值为负数,则将该 ...

  2. setInterval && setTimeout || 定时器

    来自w3school的解释 定时器setInterval() 方法可按照指定的周期(以毫秒计)来调用函数或计算表达式. setInterval() 方法会不停地调用函数,直到 clearInterva ...

  3. 关于char/varchar(n)中n的探究:字符数or字节数

    [问题来源]将设计的数据库表展示的时候,yu哥问我,你的那个top_info字段定义的类型是varchar(100),为什么是100呢,这100的长度能存多少个中文? 当时的想法就是,这个100能存多 ...

  4. information_schema.referential_constraints 学习

    information_schema.referential_constraints 表用于查看外键约束 1.information_schema.referential_constraints表的常 ...

  5. angularJS常用命令

    首先使用命令行进入你要编辑的web项目目录下: (一)编译浏览项目 1:grunt build    对web项目编译: 2:grunt server    装载(在浏览器上查看页面): 3:ctrl ...

  6. Unity3D自定义地形的笔刷,刷出别样地形

    ​ 是不是很简单呀,大家可以发挥想象刷出特殊的地形,小鸡呀,或者其他的logo之类(顶视图看上去效果很棒)的地形. 最后把我找的笔刷上传,Gizmos 注意: 如果文件夹及图片导入后,地形系统的笔刷无 ...

  7. Socket也有专门的Unicode版本

    https://www.chilkatsoft.com/refdoc/wcppCkSocketWRef.html https://www.chilkatsoft.com/refdoc/vcCkSock ...

  8. 仿新浪微博短网址PHP实现方案

    微博限制140字,但是我们知道有时需要分享一个类似淘宝商品的链接,很长,为了避免这个问题,所有了短网址的概念,废话不多说,直接把我的实现方案分享一下: 1)将长网址md5生成32位签名串,分为4段, ...

  9. jquery validationEngine的使用

    1.引入文件 <script src="/js/jquery-1.4.2.min.js" type="text/javascript"></s ...

  10. EXT JS 4.3 在线学习

    官网地址:http://docs.sencha.com/extjs/4.1.3/ 相关示例:http://docs.sencha.com/extjs/4.1.3/#!/example Examples ...