《Effective Modern C++》Item 2总结

先提出两个基本观点:

1.auto和模板参数类型推导拥有几乎一模一样的规则,所以Item1总结的规则对于auto适用。

2.auto和模板参数了类型推导有一个不相同的地方,不同在于对于花括号的处理不同。为什么不同呢?王八屁股,规定!Scotter Meyer也不知道答案。

我们知道Item1 ,提出了三个不同的case:

1.类型描述符是指针或引用,并且不是全球通引用

2.类型描述符是全球通引用

3.类型描述符既不是指针,也不是引用

template<typename T> void f(ParamType param);

简单来看,我们可以把auto当成模板函数参数描述符里的 T。

const int x=5;

auto y=x;    //y is int.

auto yy= 5; //yy is int.

const auto cy = yy;

上面代码类型推导遵循case3,所有的auto类型都是int。

const auto& ry= yy;

上面代码遵循case1.

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&&

上面遵循case2.

不多说了,重点说下区别。

auto会把花括号形式数据,转换为初始化列表。

auto x3 = { 27 }; // type is std::initializer_list<int>,
 // value is { 27 }

而这样如下方式使用模板就是错误

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

并且对于C++14来说,无法编译器无法推断花括号返回类型,包括函数和lambda。

也无法推断lamda的auto形参类型。

时间: 2024-08-02 00:23:09

《Effective Modern C++》Item 2总结的相关文章

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

条款6 当推断意外类型时使用显式的类型初始化语句 基础知识 当使用std::vector<bool>的时候,类型推断会出现问题: std::vector<bool> features(const Widget& w); // OK bool highPriority = features(w)[5]; processWidget(w, highPriority); // ERROR auto highPriority = features(w)[5]; processWid

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

条款一 了解模板类型推断 基本情况 首先定义函数模板和函数调用的形式如下,在编译期间,编译器推断T和ParamType的类型,两者基本不相同,因为ParamType常常包含const.引用等修饰符 template<typename T> void f(ParamType param); // 函数模板形式 f(expr); // 函数调用 存在T的类型即为expr类型的情况,如下T为int templat<typename T> void f(const T& param

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

条款7 辨别使用()与{}创建对象的差别 基础知识 目前已知有如下的初始化方式: int x(0); int y = 0; int z{0}; int z = {0}; // the same as above 在以“=”初始化的过程中没有调用赋值运算,如下例所示: Widget w1; // default ctor Widget w2 = w1; // copy ctor w1 = w2; // assignment, operator = 还可以用来初始化: class Widget {

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

了解decltype 基础知识 提供一个变量或者表达式,decltype会返回其类型,但是返回的内容会使人感到奇怪. 以下是一些简单的推断类型: const int i = 0; // decltype(i) -> const int bool f(const Widget& w); // decltype(w) -> const Widget&, decltype(f) -> bool(const Widget&) struct Point { int x, y

Effective Modern C++ 读书笔记 Item 1

最近发现了<Effective Modern C++>这本书,作者正是大名鼎鼎的Scott Meyers——<Effective C++>.<Effective STL>的作者. 而就在C++11逐渐普及,甚至是C++14的新特性也进入大家的视野的时候,<Effective Modern C++>一书应运而生.此书与其前辈一样,通过数十个条款来展开,只不过这次是集中于C++11和C++14的新特性.auto.decltype.move.lambda表达式……

[C++11] Effective Modern C++ 读书笔记

本文记录了我读Effective Modern C++时自己的一些理解和心得. item1:模板类型推导 1)reference属性不能通过传值参数传入模板函数.这就意味着如果模板函数需要一个reference类型的参数,必须在模板声明中将其声明为reference,否则,即使使用一个reference类型的变量调用模板函数,类型推导的结果将不带reference属性. 2)constant和volatile属性也不能通过传值参数传入模板函数,但是可以通过reference参数传入这些属性. 3

决定干点事儿--翻译一下《effective modern c++》

写了很多关于C++11的博客,总是觉得不踏实,很多东西都是东拼西凑.市场上也很少有C++11的优秀书籍,但幸运的是Meyers老爷子并没有闲赋,为我们带来了<effective modern c++>. 我们都要认清,一个人很难超越自我,超越自我的巅峰之作.因为不同的时代,也会早就不同的伟大作品. 说上面这段话的意思就是,我们不能期待<effective modern c++>能达到<effective c++>给我们带来的惊喜,但是也是出自大师之手. Learn ho

Effective Modern C++

1. Deducing Types. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Item 1: Understand template type deduction. Item 2: Understand auto type deduction. Item 3: Understand decltype

《Effective Modern C++》Item 1总结

Item 1: Understand template type deduction. 理解模板类型推导 template<typename T> void f(ParamType param); The type deduced for T is dependent not just on the type of expr, but also on the form of ParamType. 对于T类型推导,不仅依赖传入模板表达式也依赖ParamType的形式. ParamType is

《Effective Modern C++》读书笔记 Item 2 auto的类型推导

注意: 还要学习一个 ↑↑↑↑ 这样的方框里的片段完全不来自于原书,而是我自己的理解. Item 2 Understand auto type deduction - auto类型推导 在C++11之前,auto 关键字一直是用于声明自动储存类型的变量时使用的,基本上没有什么实际作用,地位和 export 关键字(用于向编译单元之外导出模板,也在C++11中被取消)类似. 在C++11中,auto 终于不再废材,终于具备了类似C#中 var 关键字的效果,可以自动推导出变量的类型,可以少打几个字