《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 a pointer or reference type, but not a universal reference. (Universal references are described in Item 24. At this point, all you need to know is that  they exist and that they’re not the same as lvalue references or rvalue references.)如果ParamType形式是一个指针或引用,并且不是全球通引用,全球通引用将在条款24介绍
  • ParamType is a universal reference.ParamType形式是全球通引用。
  • ParamType is neither a pointer nor a reference.ParamType形式既不是指针也不是引用。

Case 1: ParamType is a Reference or Pointer, but not a Universal Reference

  1. 如果传入表达式是一个引用,T忽略其引用部分
  2. 然后模式匹配表达式类型与ParamType形式,继而推导T的类型
template<typename T>
void f(T& param); // param is a reference

int x = 27; // x is an int
const int cx = x; // cx is a const int
const int& rx = x; // rx is a reference to x as a const int

f(x);  // T is int, param‘s type is int&
f(cx); // T is const int,
       // param‘s type is const int&
f(rx); // T is const int,
       // param‘s type is const int&
template<typename T>
void f(const T& param); // param is now a ref-to- const
int x = 27; // as before
const int cx = x; // as before
const int& rx = x; // as before
f(x); // T is int, param‘s type is const int&
f(cx); // T is int, param‘s type is const int&
f(rx); // T is int, param‘s type is const int&

如果ParamType形式是一个指针或指向const对象的指针,情况基本差不多。

template<typename T> void f(T* param);int x= 27;const int *px = &x; // px is a ptr to x as a const intconst int * const cpx= &x;f(&x); // T is int, param‘s type is int*f(px); // T is const int,       // param‘s type is const int*f(cpx);// T is const int,     // param‘s type is const int*

 


Case 2: ParamType is a Universal Reference

  1. 如果传入模板表达式是左值,那么T和ParamType均会推导为左值引用,这里使用引用折叠语法。
  2. 如果传入模板表达式是右值,那么T类型推导遵循case1规则。

Case 3: ParamType is Neither a Pointer nor a Reference

template<typename T> void f(T param);
  1. 如果传入模板表达式类型是引用,T的类型忽略引用。
  2. 如果表达式剩余部分是const,T也忽略掉,是volatile,T也忽略掉。
int x = 27; // as before
const int cx = x; // as before
const int& rx = x; // as before
f(x); // T‘s and param‘s types are both int
f(cx); // T‘s and param‘s types are again both int
f(rx); // T‘s and param‘s types are still both int

还有顶层const需要忽略,而底层const保留

template<typename T>
void f(T param); // param is still passed by value
const char* const ptr = // ptr is const pointer to const object
 "Fun with pointers";
f(ptr); // pass arg of type const char * const

T,param type is const char*

写到这里吧,本来想翻译这本书,翻译一段,就只想翻译Item1,翻译Item1一半,我放弃了,原谅我吧。

向大家推荐这么书,里面有42条款,这么书作者是大名鼎鼎《Effective C++》 Scotter Meyer写的,主要讲C++11/14,与时俱进嘛。

时间: 2024-07-30 10:16:57

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

[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 2 auto的类型推导

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