12.26

Effective C++ Scott Meyers

Chapter 4. Designs and Declarations

1. Item 25: Consider support for a non-throwing swap.

  Provide a swap member function when std::swap would be inefficient for your type. Make sure your swap doens't throw exceptions.

  If you offer a member swap, also offer a non-member swap that calls the member. For classes(not templates), specialize std::swap, too.

  When calling swap, employ a using declaration for std::swap, them call swap without namespace qualification.

  It's fine to totally specialize std templates for user-defined types, but never try to add something completely new to std.

 

Chapter 5. Implementations

2. Item 26: Postpone variable definitions as long as possible.

  Postpone variable definitions as long as possible. It increases program clarity and improves program efficiency.

3. Item 27: Minimize casting.

  Avoid casts whenever practical, especially dynamic_casts in performance-sensitive code. If a design requires casting, try to develop a cast-free alternative.

  When casting is necessary, try to hide it inside a function. Clients can then call the function instead of putting casts in their own code.

  Prefer C++ style casts to old-style casts. They are easier to see, and they are more specific about what they do.

3. Item 28: Avoid returning "handles" to object internals.

  Avoid returning handles (references, pointers, or iterators) to object internals. Not returning handles increases encapsulation, helps const member functions act const, and minimizes the creation of dangling handles.

4. Item 29: Strive for exception-safe code.

  Exception-safe functions leak no resources and allow no data structures to become corrupted, even when exceptions are thrown. Such functions offer the basic, strong, or nothrow guarantees.

  The strong guarantee can often be implemented via copy-and-swap, but the strong guarantee is not practical for all functions.

  A function can usually offer a guarantee no stronger than the weakest guarantee of the functions it calls.

5. Item 30: Understand the ins and outs of inlining.

   

  

 

posted @ 2018-12-26 16:28  lefthook  阅读(95)  评论(0编辑  收藏  举报