条款二 了解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. poi HSSFRichTextString 对cell中的每一段文字设置字体

    HSSFRichTextString ts= new HSSFRichTextString(" 经审核,我司已同意其出库申请.请你部按规定将托管银行承兑汇票办理出库." + &qu ...

  2. CDZSC_2015寒假新人(1)——基础 g

    Description Ignatius likes to write words in reverse way. Given a single line of text which is writt ...

  3. jquery的使用 超级快速入门 熟练使用

    如何使用jquery,首先需要引入jquery的js库文件,可以是免费的cdn资源,也可以是本地下载的资源 使用方法:$(function(){                  这里面写你要执行的代 ...

  4. PHP根据经纬度,计算2点之间的距离的2种方法

    计算地球表面2点之间的球面距离 /** * @param $lat1 * @param $lng1 * @param $lat2 * @param $lng2 * @return int */ fun ...

  5. [C++程序设计]引用

    对一个数据可以使用“引用”(reference),这是C++对C的一个重要扩充,引用是一种新的变量类型,它的作用是为一个变量起一个别名.假如有一个变量a,想给它起一个别名b,可以这样写:int a; ...

  6. kibana 日志查看界面

  7. ###Android 断点调试和高级调试###

    转自:http://www.2cto.com/kf/201506/408358.html 有人说Android 的调试是最坑的,那我只能说是你不会用而已,我可以说Android Studio的调试是我 ...

  8. Python 列表生成式、生成器、迭代器

    列表生成式 列表生成式即List Comprehensions,是Python内置的非常简单却强大的可以用来创建list的生成式. 如果要生成[1x1, 2x2, 3x3, ..., 10x10]怎么 ...

  9. H5实现图片优化上传

    一,HTML部分 <input type="file" accept="images/*"> <input class="url&q ...

  10. C++编程规范和标准总结

    文件名: 每个源代码文件应该有一个包含文件.每个包含文件描述了单个类或者多个类相结合的集合.一般头文件(.h,或.hpp)包含类的定义而不是实例.因此包含文件可以用在多个文件当中,源文件(.c,.或c ...