条款二 了解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. iOS-设计模式之Block

    Block是代码块, Block定义 返回值 (^ 块名)(参数1,参数2…); 在定义Block的时候可以使用typedef 重命名一下. typedef void(^blockName)(NSSt ...

  2. ural 1297 Palindrome(Manacher模板题)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud 求最长回文子串. http://acm.timus.ru/problem.aspx ...

  3. AnyWhere

    anyexec_date 201506171258 anyexec_date @@AnyWhereCmd idc python /home/appadmin/nicker/tools/QUI.py m ...

  4. (原)Windows下编译指纹识别的Rel_4.1.0库

    网址:http://www.cnblogs.com/darkknightzh/p/4867372.html.未经允许,严禁转载. 没怎么用过linux,对于MSYS和MinGW也基本没用过,因而编译R ...

  5. easyui datagrid detailview嵌套datagrid的问题

    解决问题办法来自 http://www.coding123.net/article/20141113/easyui-datagrid-datailview-use-sub-datagrid-not-a ...

  6. php常用mysql函数

    mysql_affected_rows: 得到 MySQL 最后操作影响的列数目. mysql_close: 关闭 MySQL 伺服器连线. mysql_connect: 开启 MySQL 伺服器连线 ...

  7. 12个非常有用的JavaScript小技巧

    在这篇文章中将给大家分享12个有关于JavaScript的小技巧.这些小技巧可能在你的实际工作中或许能帮助你解决一些问题. 使用!!操作符转换布尔值 有时候我们需要对一个变量查检其是否存在或者检查值是 ...

  8. [c language] getopt 其参数optind 及其main(int argc, char **argv) 参数解释

    getopt被用来解析命令行选项参数.#include <unistd.h> extern char *optarg; //选项的参数指针extern int optind, //下一次调 ...

  9. vs2003的代码考到vs2010 会出现(Windows CR LF)

    拷贝到VS中,出现各种莫名其妙的编译错误,这个时候需要注意,造成这种情况的原因可能在于,你所拷贝的源代码在换行时并非采用Windows系统的CR LF方式. 下面给出一个简单的方法来将不合要求的文本更 ...

  10. python笔记之ZipFile模块

    python笔记之ZipFile模块 zipfile模块用来做zip格式编码的压缩和解压缩的,zipfile里有两个非常重要的class, 分别是ZipFile和ZipInfo, 在绝大多数的情况下, ...