13.10 建议
[1] 对于库来说,不是大而复杂才会有用;13.1节。
[2] 所谓资源是指需要先申请后(显式或隐式)释放的东西;13.2节。
[3] 用资源句柄来管理资源(RAII);13.2节;[CG:R.1]。
[4] 用unique_ptr引用多态类型的对象;13.2.1节;[CG:R.20]。
[5] shared_ptr(仅)用于引用共享的对象;13.2.1节;[CG:R.20]。
[6] 与智能指针相比,优先选择具有特定语义的资源句柄;13.2.1节。
[7] 与shared_ptr相比,优先选择unique_ptr;5.3节、13.2.1节。
[8] 使用make_unique()构造unique_ptr;13.2.1节;[CG:R.22]。
[9] 使用make_shared()构造shared_ptr;13.2.1节;[CG:R.23]。
[10] 与垃圾回收机制相比,优先选择智能指针;5.3节,13.2.1节。
[11] 不要使用std::move();13.2.2节;[CG:ES.56]。
[12] 只使用std::forward()进行转发;13.2.2节。
[13] 在std::move()或std::forward()之后绝不从对象读取数据;13.2.2节。
[14] 与指针加计数的接口相比,优先选择span;13.3节;[CG:F.24]。
[15] 在需要序列的大小为constexpr的地方使用array;13.4.1节。
[16] 与内置数组相比,优先选择array;13.4.1节;[CG:SL.con.2]。
[17] 如果你需要使用N个二进制位,而N又并非恰好是某种内置整数类型的大小,则使用bitset;13.4.2节。
[18] 不要过度使用pair和tuple;命名struct通常会产生更易读的代码;13.4.3节。 ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access