《Effective Modern C++》- 极精简版 1-4条

本文概述了《EffectiveModernC++》中的模板类型推导、auto类型推导、decltype的理解以及如何查看类型推导结果。强调了记忆推导规则的困难和现有工具的局限性,推荐通过实践和使用现有的推导结果来提升效率。
摘要由CSDN通过智能技术生成

         本文章属于专栏- 概述 -《业界C++进阶建议整理》-CSDN博客


         本文列出《Effective Modern C++》的1-4条的个人理解的极精简版本。

  • Item1、模版类型推导
    • 个人理解:推导规则是繁琐的,和运算符优先级一样,背下来没有意义,最好的办法是在使用中熟悉。常用的推导结果如下表1。目前没有一个完全正确的打印推导结果工具(这是模板使用和调试困难的核心原因之一,个人觉得这是未来c++标准委员会设计时必须要考虑改进的点),所以要么深刻理解推导规则,要么使用别人建立的推导结果表,对大部分人,我的建议是后者。
    • 表1
      PAT
      1Tintint
      2Tint*int*
      3Tint&int
      4Tconst intint
      5Tconst int *const int *
      6Tint * constint *
      7Tconst int &int
      8Tconst int * constconst int *
      9Tchar [2]char *
      10Tconst char [12]const char *
      11Tvoid (int)void (*)(int)
      12const Tintint
      13const Tint *int *
      14const Tint &int
      15const Tconst intint
      16const Tconst int *const int *
      17const Tconst int &int
      18const Tconst int * constconst int *
      19const Tchar [2]char *
      20const Tconst char [12]const char *
      21const Tvoid (int)void (*)(int)
      22T&intint
      23T&int *int *
      24T&int &int
      25T&const intconst int
      26T&const int *const int *
      27T&const int &const int
      28T&const int * constconst int * const
      29T&char [2]char [2]
      30T&const char [12]const char [12]
      31T&void (int)void (int)
      32T&&intint &
      33T&&int *int *&
      34T&&int &int &
      35T&&const intconst int &
      36T&&const int *const int *&
      37T&&const int &const int &
      38T&&const int * constconst int * const &
      39T&&char [2]char (&)[2]
      40T&&const char [12]const char (&)[12]
      41T&&void (int)void (&)(int)
      42T&&int &&int
      43const T&int &&int
  • Item2、理解auto类型推导
    • auto的推导与模版的推导方式只有一个不同,auto可以推导出std::initializer_list,模版推导不行。
    • 另外注意auto在c++11中不能用于返回值推导,但是c++14可以,但是返回值不可被推导为std::initializer_list
  • Item3、理解decltype
    • 返回名字或者表达式类型,它不会丢掉引用。
    • 下图是容易理解的推导结果
    • 想用decltype在模版中表示返回值类型
      • 这里需要注意几个点
        • 输入参数是Container&&,这里使用了通用引用,是为了函数传入参数可以是左值引用,也可以是右值引用
    • 在c++14中,想在auto推导时,不丢掉引用,就用decltype(auto)
    • decltype陷阱
      • decltype对于表达式,会把类型定义为返回类型的引用。在c++11中。int x; x是变量,(x)是表达式,所以decltype(x)返回int,decltype((x))返回int&。特别小心不要返回局部变量的引用
  • Item4、查看类型推导的结果
    • 通过编译器报错
    • 通过运行时输出
      • 使用typeid(T).name。但是这里会像传值一样推导,会丢掉引用、const、volatile部分
      • Boost.TypeIndex是更好的选择
    • 注意,目前所有的工具都可能出现问题,目前只有深刻理解推导规则或者看已有的推导结果表

 

If you’re an experienced C++ programmer and are anything like me, you initially approached C++11 thinking, “Yes, yes, I get it. It’s C++, only more so.” But as you learned more, you were surprised by the scope of the changes. auto declarations, range-based for loops, lambda expressions, and rvalue references change the face of C++, to say nothing of the new concurrency features. And then there are the idiomatic changes. 0 and typedefs are out, nullptr and alias declarations are in. Enums should now be scoped. Smart pointers are now preferable to built-in ones. Moving objects is normally better than copying them. There’s a lot to learn about C++11, not to mention C++14. More importantly, there’s a lot to learn about making effective use of the new capabilities. If you need basic information about “modernC++ features, resources abound, but if you’re looking for guidance on how to employ the features to create software that’s correct, efficient, maintainable, and portable, the search is more challenging. That’s where this book comes in. It’s devoted not to describing the features of C++11 and C++14, but instead to their effective application. The information in the book is broken into guidelines called Items. Want to understand the various forms of type deduction? Or know when (and when not) to use auto declarations? Are you interested in why const member functions should be thread safe, how to implement the Pimpl Idiom using std::unique_ptr, why you should avoid default capture modes in lambda expressions, or the differences between std::atomic and volatile? The answers are all here. Furthermore, they’re platform-independent, Standards-conformant answers. This is a book about portable C++.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值