当前位置: 代码迷 >> 综合 >> Modern Effective C++
  详细解决方案

Modern Effective C++

热度:90   发布时间:2023-11-24 15:26:04.0

Effective Modern C++

Deducing Types

类型推断

Item 1: Understand template type deduction

条款1:理解模板模型推断

Item 2: Understand auto type deduction

条款2:理解auto类型推断

Item 3: Understand decltype

条款3:理解decltype

Item 4: Know how to view deduced types

条款4: 掌握预览推断类型

auto

atuo

Item 5: Prefer auto to explicit type declarations

条款5:优先使用auto而非显示类型声明

Item 6: Use the explicitly typed initializer idiom when auto deduces undesired types

条款6:当无法auto推断类型时使用显示类型初始化习惯

Moving to Modern C++

转向现代C++

Item 7: Distinguish between () and {} when creating objects

条款7:创建对象时区别()和{}

Item 8: Prefer nullptr to 0 and NULL

条款8:优先使用nullptr而非0或NULL

Item 9: Prefer alias declarations to typedefs

条款9:优先使用别名声明而非typedef

Item 10: Prefer scoped enums to unscoped enums

优先使用作用范围内枚举而非不限作用域内枚举

Item 11: Prefer deleted functions to private undefined ones

条款11:优先使用删除函数而非private未定义函数

Item 12: Declare overriding functions override

条款12:添加重写函数声明override

Item 13: Prefer const_iterators to iterators

条款13:优先使用const_iterator而非iterator

Item 14: Declare functions noexcept if they won’t emit exceptions

条款14:如果不会发生异常将函数声明为非异常函数

Item 15: Use constexpr whenever possible

条款15:尽可能使用常量表达式

Item 16: Make const member functions thread safe.

保证const成员函数线程安全

Item 17: Understand special member function gene

条款17:理解特殊成员函数机制

Smart Pointers

智能指针

Item 18: Use std::unique_ptr for exclusive-ownership resource management

条款18:使用std::unique_ptr指针管理专有资源

Item 19: Use std::shared_ptr for shared-ownership resource management

条款19:使用std::shared_ptr指针管理共享资源

Item 20: Use std::weak_ptr for std::shared_ptr-like pointers that can dangle

条款20:使用std::weak_ptr管理可能悬空指针

Item 21: Prefer std::make_unique and std::make_shared to direct use of new

条款21:优先std::mkae_unique和std::mkae_shared而非直接使用new

Item 22: When using the Pimpl Idiom, define special member functions in the implementation file

条款22:当使用Pimpl习惯时,将定义的特殊成员函数放在实现文件中

Rvalue References, Move Semantics, and Perfect Forwarding

右值引用,移动语义,完美转发

Item 23: Understand std::move and std::forward

条款23:理解std::move和Std::forward

Item 24: Distinguish universal references from rvalue references

条款24:区分通用引用和右值引用

Item 25: Use std::move on rvalue references, std::forward on universal references

条款25:使用std::move在右值引用,std::forward使用于通用引用

Item 26: Avoid overloading on universal references

条款26:避免在通用引用中重载

Item 27: Familiarize yourself with alternatives to overloading on universal references

条款27:在通用引用中熟悉重载替代方案

Item 28: Understand reference collapsing

条款28:理解引用折叠

Item 29: Assume that move operations are not present, not cheap, and not used

条款29:假定魔洞操作不存在,成本高,未使用

Item 30: Familiarize yourself with perfect forwarding failure cases

条款30:熟悉优先使用完美转换失败情形

Lambda Expressions

Lambda表达式

Item 31: Avoid default capture modes

条款31:避免默认捕捉模式

Item 32: Use init capture to move objects into closures

条款32:使用初始化捕捉将对象移到闭包

Item 33: Use decltype on auto&& parameters to std::forward them

条款33:使用decltype在atuo&&参数实现std::forward

Item 34: Prefer lambdas to std::bind

条款34:优先lambda而非std::bind

The Concurrency API

并发API

Item 35: Prefer task-based programming to thread-based

条款35:优先基于任务编程而非基于线程

Item 36: Specify std::launch::async if asynchronicity is essential

条款36:如果异步是必要的,使用std::launch::async

Item 37: Make std::threads unjoinable on all paths

条款37:使用std::thread对象路径不可联结

Item 38: Be aware of varying thread handle destructor behavior

条款38:关注变化线程句柄的析构行为

Item 39: Consider void futures for one-shot event communication

条款39:考虑一次性事件通信以void作为实参的期值

Item 40: Use std::atomic for concurrency, volatile for special memory

条款40:使用std::atomic于并发,对特殊内存使用volatile

Tweaks

变动

Item 41: Consider pass by value for copyable parameters that are cheap to move and always copied

条款41:对于可复制的参数在低成本移动和复制中使用按值传递

Item 42: Consider emplacement instead of insertion

条款42:考虑置入而非插入