跳至主要內容
  • Hostloc 空間訪問刷分
  • 售賣場
  • 廣告位
  • 賣站?

4563博客

全新的繁體中文 WordPress 網站
  • 首頁
  • 《Effective Modern C++》中“Item 3: Understand decltype”章节读书笔记
未分類
30 1 月 2021

《Effective Modern C++》中“Item 3: Understand decltype”章节读书笔记

《Effective Modern C++》中“Item 3: Understand decltype”章节读书笔记

資深大佬 : XIVN1987 8

《 Effective Modern C++》真是本好书,,讲的深入浅出,非常清晰

decltype

Given a name or an expression, decltype tells you the name’s or the expression’s type

const int i = 0;            // decltype(i) is const int  bool f(const Widget& w);    // decltype(w) is const Widget&                             // decltype(f) is bool(const Widget&)  template<typename T> class vector { public:     T& operator[](std::size_t index); }; vector<int> v;              // decltype(v) is vector<int>                             // decltype(v[0]) is int& 

如何查看 decltype(f)、decltype(v[0])的值?

template<typename T> class TD;                   // Type Displayer  TD<decltype(v[0])> x; 

编译如上代码,编译器会报错如下:

error: aggregate 'TD<int&> x' has incomplete type and cannot be defined 

由此可知 decltype(v[0])的结果是int&

decltype(auto)

auto specifies that the type is to be deduced, and decltype says that decltype rules should be used during the deduction

template<typename Container, typename Index> decltype(auto) authAndAccess(Container& c, Index i) {     authenticateUser();     return c[i]; }  

这里之所以使用 decltype(auto)而不是 auto,是因为绝大多数 containers-of-T 的[]运算符返回 T&,用 auto 的话会丢失&属性

decltype((name))

if an lvalue expression other than a name has type T, decltype reports that type as T&

int x = 3;      // decltype(x)   is int                 // decltype((x)) is int& 

注意:decltype(-x)等不会出现这种问题,因为-x等表达式都是右值,只有(x)既是表达式还是左值

这将导致下面两个函数的返回值类型不同

decltype(auto) f1() {     int x = 0;     return x; }  decltype(auto) f2() {     int x = 0;     return (x); } 

大佬有話說 (5)

  • 資深大佬 : jones2000

    这个是 c++几的特性?
    编译器要什么版本的?

  • 資深大佬 : linux40

    @jones2000 decltype(auto) 这种写法起码 C++14 。

  • 主 資深大佬 : XIVN1987

    @jones2000
    C++14

    C++14 是个小升级,相对于 C++11 添加的特性不多,,
    如果能用 C++11 的话,建议用 C++14,,毕竟 C++14 里有 std::make_unique

  • 資深大佬 : auto8888

    哇撒,发现公司 gcc 版本升级到 7.5.0 了,可以玩 c++17 了,爽

  • 資深大佬 : willhunger

    可以更仔细一点:

    如果函数返回值只用 auto 的话,采用的是模板类型推导,而函数模板类型推导中如果函数形参为引用类型(非万能引用类型)的话,会忽略其 & 属性。故才需要采用 decltype(auto)。

文章導覽

上一篇文章
下一篇文章

AD

其他操作

  • 登入
  • 訂閱網站內容的資訊提供
  • 訂閱留言的資訊提供
  • WordPress.org 台灣繁體中文

51la

4563博客

全新的繁體中文 WordPress 網站
返回頂端
本站採用 WordPress 建置 | 佈景主題採用 GretaThemes 所設計的 Memory
4563博客
  • Hostloc 空間訪問刷分
  • 售賣場
  • 廣告位
  • 賣站?
在這裡新增小工具