Organized in a practical problem-and-solution format, More Exceptional C++ picks up where the widely acclaimed Exceptional C++ leaves off, providing successful strategies for solving real-world problems in C++. Drawing from years of in-the-trenches experience, Herb Sutter provides tested techniques and practical solutions for programmers designing modern software systems with C++, from small projects to enterprise applications.
Built around forty programming puzzles, More Exceptional C++ helps you understand the rules and issues critical to successful software design and development in C++. New themes included in this sequel place a strong emphasis on generic programming, memory management, and using the C++ standard library, including coverage of important techniques like traits and predicates. Also included are guidelines and considerations to remember when using standard containers and algorithms--topics rarely covered in-depth in other sources.
Readers will find solutions to such important questions as:
What pitfalls might you encounter when using std::map and std::set, and how can you safely avoid them?
What kinds of predicates are safe to use with the STL, what kinds aren't, and why?
What techniques are available for writing powerful generic template code that can change its own behavior based on the capabilities of the types it's given to work with?
When and how should you optimize your code? Why can (and do) fancy optimizations get us into trouble? And how can some of these answers change if you're writing multithread-safe code?
Does exception safety affect class design, or can it be retrofitted in as an afterthought?
How can you avoid the Siamese Twin problem when combining inheritance-based libraries from different vendors?
How can you safely use auto_ptr, and then use common design patterns to adapt it to avoid common pitfalls? Can you use auto_ptr as a class member? What must you know before you elect to use it that way?
Plus one of the most frequently recurring questions about modern C++: When and how should you use namespaces, anyway?
A must-have for the serious programmer, More Exceptional C++ provides a thorough and pragmatic understanding of the language while showing you how to write exceptional code in C++.
Table of Contents
Generic Programming and the C++ Standard Library
- 1. Switching Streams
- 2. Predicates, Part 1: What remove() Removes
- 3. Predicates, Part 2: Matters of State
4. Extensible Templates: Via Inheritance or Traits?
- Requiring Member Functions
- Constraints Classes
- Requiring Inheritance, Take 1: IsDerivedFrom1 Value Helper
- Requiring Inheritance, Take 2: IsDerivedFrom2 Constraints Base Class
- Requiring Inheritance, Take 3: A Merged IsDerivedFrom
- Selecting Alternative Implementations
- Requirements versus Traits
- Inheritance versus Traits
- 5. Typename
- 6. Containers, Pointers, and Containers That Aren't
- 7. Using vector and deque
- 8. Using set and map
- 9. Equivalent Code?
- 10. Template Specialization and Overloading
- 11. Mastermind
Optimization and Performance
- 12. inline
- 13. Lazy Optimization, Part 1: A Plain Old String
- 14. Lazy Optimization, Part 2: Introducing Laziness
- 15. Lazy Optimization, Part 3: Iterators and References
- 16. Lazy Optimization, Part 4: Multithreaded Environments
Exception Safety Issues and Techniques
- 17. Constructor Failures, Part 1: Object Lifetimes
- 18. Constructor Failures, Part 2: Absorption?
- 19. Uncaught Exceptions
- 20. An Unmanaged Pointer Problem, Part 1: Parameter Evaluation
- 21. An Unmanaged Pointer Problem, Part 2: What About auto_ptr?
22. Exception-Safe Class Design, Part 1: Copy Assignment
- Review: Exception Safety Canonical Forms
- Analyzing the Cargill Widget Example
- A General Technique: Using the Pimpl Idiom
- A Potential Objection, and Why It's Unreasonable
- Conclusion 1: Exception Safety Affects a Class's Design
- Conclusion 2: You Can Always Make Your Code (Nearly) Strongly Exception-Safe
- Conclusion 3: Use Pointers Judiciously
- 23. Exception-Safe Class Design, Part 2: Inheritance
Inheritance and Polymorphism
- 24. Why Multiple Inheritance?
- 25. Emulating Multiple Inheritance
- 26. Multiple Inheritance and the Siamese Twin Problem
- 27. (Im)pure Virtual Functions
- 28. Controlled Polymorphism
Memory and Resource Management
29. Using auto_ptr
- Problem: Arrays and auto_ptr Don't Mix
- Aside on a Non-problem: Zero-Length Arrays Are Okay
- Option 1: Roll Your Own auto_array
- Option 2: Use the Adapter Pattern (Score: 7 / 10)
- Option 3: Replace auto_ptr with Hand-Coded Exception-Handling Logic (Score: 1 / 10 )
- Option 4: Use a vector Instead of an Array (Score: 9.5 / 10 )
- 30. Smart Pointer Members, Part 1: A Problem with auto_ptr
- 31. Smart Pointer Members, Part 2: Toward a ValuePtr
- 29. Using auto_ptr
Free Functions and Macros
- 32. Recursive Declarations
- 33. Simulating Nested Functions
- 34. Preprocessor Macros
- Common Macro Pitfalls
- Other Macro Drawbacks
- There Are Some Things Even a Macro Won't Do
- 36. Initialization
- 37. Forward Declarations
- 38. Typedef
- 39. Namespaces, Part 1: Using-Declarations and Using-Directives
40. Namespaces, Part 2: Migrating to Namespaces
- Migrating to Namespaces, Safely and Effectively
Guidelines for a Good Long-Term Solution
- Namespace Rule #1: Never write using-directives in header files.
- Namespace Rule #2: Never write namespace using-declarations in header files.
- Namespace Rule #3: In implementation files, never write a using-declaration or a using-directive before an #include directive.
- “Kinda Sorta” Namespace Rule #4: Use C headers with the new style #include <cheader> instead of the old style #include <header.h>.
- Long-Term Approaches: A Motivating Example
- A Good Long-Term Solution
- A Not-So-Good Long-Term Solution
- An Effective Short-Term Solution
- Conclusion: Migrating to the Long-Term Solution
A. Optimizations That Aren't (in a Multithreaded World)
- What's the Fuss About?
- Recap: Plain Old Original:: String (Item 13)
- Using a Plain Old String: Single-Threaded
- Using a Plain Old String: Multithreaded
- Now Throw in an “Optimization”: Copy-On-Write (COW)
- Using a COW String: Multithreaded
- Mild versus Severe Inefficiencies
- Some Actual Numbers
- A Real-World Example: C++'s basic_string
- “Am I Affected?”
- B. Test Results for Single-Threaded versus Multithread-Safe String Implementations
- Title: More Exceptional C++
- Release date: December 2001
- Publisher(s): Addison-Wesley Professional
- ISBN: 020170434X