You are previewing Exceptional C++: 47 Engineering Puzzles, Programming Problems, and Solutions.
O'Reilly logo
Exceptional C++: 47 Engineering Puzzles, Programming Problems, and Solutions

Book Description

Exceptional C++ shows by example how to go about sound software engineering in standard C++. Do you enjoy solving thorny C++ problems and puzzles? Do you relish writing robust and extensible code? Then take a few minutes and challenge yourself with some tough C++ design and programming problems.

The puzzles and problems in Exceptional C++ not only entertain, they will help you hone your skills to become the sharpest C++ programmer you can be. Many of these problems are culled from the famous Guru of the Week feature of the Internet newsgroup comp.lang.c++.moderated, expanded and updated to conform to the official ISO/ANSI C++ Standard.

Each problem is rated according to difficulty and is designed to illustrate subtle programming mistakes or design considerations. After you've had a chance to attempt a solution yourself, the book then dissects the code, illustrates what went wrong, and shows how the problem can be fixed. Covering a broad range of C++ topics, the problems and solutions address critical issues such as:

  • Generic programming and how to write reusable templates

  • Exception safety issues and techniques

  • Robust class design and inheritance

  • Compiler firewalls and the Pimpl Idiom

  • Name lookup, namespaces, and the Interface Principle

  • Memory management issues and techniques

  • Traps, pitfalls, and anti-idioms

  • Optimization

  • Try your skills against the C++ masters and come away with the insight and experience to create more efficient, effective, robust, and portable C++ code.



    0201615622B04062001

    Table of Contents

    1. Copyright
      1. Dedication
    2. Foreword
    3. Preface
      1. How to Read This Book
      2. How We Got Here: GotW and PeerDirect
      3. Acknowledgments
    4. Generic Programming and the C++ Standard Library
      1. 1. Iterators
        1. Solution
      2. 2. Case-Insensitive Strings—Part 1
        1. Solution
      3. 3. Case-Insensitive Strings—Part 2
        1. Solution
      4. 4. Maximally Reusable Generic Containers—Part 1
        1. Solution
      5. 5. Maximally Reusable Generic Containers—Part 2
        1. Solution
          1. Copy Construction and Copy Assignment
          2. Usability Issues for Construction and Assignment
        2. Alternative: The Standard Library Approach
          1. Why Write the Default Constructor?
          2. A Lingering Problem
      6. 6. Temporary Objects
        1. Solution
      7. 7. Using the Standard Library (or, Temporaries Revisited)
        1. Solution
    5. Exception-Safety Issues and Techniques
      1. 8. Writing Exception-Safe Code—Part 1
        1. Solution
          1. Default Construction
          2. Destruction
      2. 9. Writing Exception-Safe Code—Part 2
        1. Solution
          1. Copy Construction
          2. Copy Assignment
      3. 10. Writing Exception-Safe Code—Part 3
        1. Solution
          1. Count()
          2. Push()
          3. Pop() Goes the Weasel
          4. The Real Problem
      4. 11. Writing Exception-Safe Code—Part 4
        1. Solution
      5. 12. Writing Exception-Safe Code—Part 5
        1. Solution
          1. Constructor
          2. Destructor
          3. Swap
      6. 13. Writing Exception-Safe Code—Part 6
        1. Solution
          1. The Default Constructor
          2. The Destructor
          3. The Copy Constructor
          4. Elegant Copy Assignment
          5. Stack<T>::Count()
          6. Stack<T>::Push()
          7. Stack<T>::Top()
          8. Stack<T>::Pop()
      7. 14. Writing Exception-Safe Code—Part 7
        1. Solution
      8. 15. Writing Exception-Safe Code—Part 8
        1. Solution
      9. 16. Writing Exception-Safe Code—Part 9
        1. Solution
          1. Destructors That Throw and Why They're Evil
          2. Safe Exceptions
      10. 17. Writing Exception-Safe Code—Part 10
        1. Solution
          1. Exception Safety and the Standard Library
      11. 18. Code Complexity—Part 1
        1. Solution
          1. Nonexceptional Code Paths
          2. Exceptional Code Paths
      12. 19. Code Complexity—Part 2
        1. Solution
    6. Class Design and Inheritance
      1. 20. Class Mechanics
        1. Solution
      2. 21. Overriding Virtual Functions
        1. Solution
      3. 22. Class Relationships—Part 1
        1. Solution
      4. 23. Class Relationships—Part 2
        1. Solution
      5. 24. Uses and Abuses of Inheritance
        1. Solution
          1. Conclusion
      6. 25. Object-Oriented Programming
        1. Solution
    7. Compiler Firewalls and the Pimpl Idiom
      1. 26. Minimizing Compile-time Dependencies—Part 1
        1. Solution
      2. 27. Minimizing Compile-time Dependencies—Part 2
        1. Solution
      3. 28. Minimizing Compile-time Dependencies—Part 3
        1. Solution
      4. 29. Compilation Firewalls
        1. Solution
      5. 30. The “Fast Pimpl” Idiom
        1. Solution
    8. Name Lookup, Namespaces, and the Interface Principle
      1. 31. Name Lookup and the Interface Principle—Part 1
        1. Solution
      2. 32. Name Lookup and the Interface Principle—Part 2
        1. Solution
          1. A Deeper Look at Koenig Lookup
          2. More Koenig Lookup: The Myers Example
      3. 33. Name Lookup and the Interface Principle—Part 3
        1. Solution
          1. What Does a Class Depend On?
          2. Some Interesting (and Even Surprising) Results
      4. 34. Name Lookup and the Interface Principle—Part 4
        1. Solution
          1. Name Hiding
          2. How to Work Around Unwanted Name Hiding
          3. Namespaces and the Interface Principle
          4. Name Hiding in Nested Namespaces
          5. Some Fun with Compilers
          6. The Solution
    9. Memory Management
      1. 35. Memory Management—Part 1
        1. Solution
      2. 36. Memory Management—Part 2
        1. Solution
      3. 37. AUTO_PTR
        1. Solution
          1. Why “Auto” Pointer?
          2. Wrapping Pointer Data Members
          3. Ownership, Sources, and Sinks
          4. The Scoop on Nonowning auto_ptrs
          5. Wrapping Pointer Members
          6. auto_ptr and Exception Safety
          7. The const auto_ptr Idiom
    10. Traps, Pitfalls, and Anti-Idioms
      1. 38. Object Identity
        1. Solution
          1. Exception Safety (Murphy)
          2. Operator Overloading (Machiavelli?)
            1. Postscript #1:
            2. Postscript #2
      2. 39. Automatic Conversions
        1. Solution
      3. 40. Object Lifetimes—Part 1
        1. Solution
      4. 41. Object Lifetimes—Part 2
        1. Solution
          1. Problem 1: It Can Slice Objects
          2. Problem 2: It's Not Exception-Safe
          3. Problem 3: It Changes Normal Object Lifetimes
          4. Problem 4: It Can Still Break Derived Classes
          5. Problem 5: this != &other
    11. Miscellaneous Topics
      1. 42. Variable Initialization—Or is it?
        1. Solution
      2. 43. Const-Correctness
        1. Solution
      3. 44. Casts
        1. Solution
      4. 45. BOOL
        1. Solution
          1. Option 1: typedef (score: 8.5 / 10)
          2. Option 2: #define (score: 0 / 10)
          3. Option 3: enum (score: 9 / 10)
          4. Option 4: Class (score: 9 / 10)
      5. 46. Forwarding Functions
        1. Solution
      6. 47. Control Flow
        1. Solution
      7. Afterword
    12. Bibliography