Exceptional C++ Style 40 New Engineering Puzzles, Programming Problems, and Solutions

Book description

Software "style" is about finding the perfect balance between overhead and functionality... elegance and maintainability... flexibility and excess. In Exceptional C++ Style, legendary C++ guru Herb Sutter presents 40 new programming scenarios designed to analyze not only the what but the why and help you find just the right balance in your software.

Organized around practical problems and solutions, this book offers new insight into crucial C++ details and interrelationships, and new strategies for today's key C++ programming techniques--including generic programming, STL, exception safety, and more. You'll find answers to questions like:

  • What can you learn about library design from the STL itself?

  • How do you avoid making templated code needlessly non-generic?

  • Why shouldn't you specialize function templates? What should you do instead?

  • How does exception safety go beyond try and catch statements?

  • Should you use exception specifications, or not?

  • When and how should you "leak" the private parts of a class?

  • How do you make classes safer for versioning?

  • What's the real memory cost of using standard containers?

  • How can using const really optimize your code?

  • How does writing inline affect performance?

  • When does code that looks wrong actually compile and run perfectly, and why should you care?

  • What's wrong with the design of std::string?

  • Exceptional C++ Style will help you design, architect, and code with style--and achieve greater robustness and performance in all your C++ software.



    Table of contents

    1. Copyright
      1. Dedication
    2. The C++ In-Depth Series
      1. Titles in the Series
    3. Preface
      1. Style or Substance?
      2. The Exceptional Socrates
      3. What I Assume You Know
      4. How to Read This Book
    4. ##. The Topic of This Item
      1. Acknowledgments
    5. Generic Programming and the C++ Standard Library
      1. 1. Uses and Abuses of vector
        1. Solution
          1. Accessing Vector Elements
          2. Size-ing Up Vector
          3. Summary
      2. 2. The String Formatters of Manor Farm, Part 1: sprintf
        1. Solution
          1. The Joys and Sorrows of sprintf
      3. 3. The String Formatters of Manor Farm, Part 2: Standard (or Blindingly Elegant) Alternatives
        1. Solution
          1. Alternative #1: snprintf
          2. Alternative #2: std::stringstream
          3. Alternative #3: std::strstream
          4. Alternative #4: boost::lexical_cast
          5. Summary
      4. 4. Standard Library Member Functions
        1. Solution
          1. Fun with mem_fun
          2. Use mem_fun, Just Not with the Standard Library
          3. Use Pointers to Member Functions, Just Not with the Standard Library
          4. Summary
      5. 5. Flavors of Genericity, Part 1: Covering the Basis [sic]
        1. Solution
      6. 6. Flavors of Genericity, Part 2: Generic Enough?
        1. Solution
      7. 7. Why Not Specialize Function Templates?
        1. Solution
          1. Why Not Specialize: The Dimov/Abrahams Example
          2. Important Morals
          3. Summary
      8. 8. Befriending Templates
        1. Solution
          1. The Original Attempt
          2. Why It's Legal But Dark
          3. Issue 1: It Doesn't Always Work
          4. Issue 2: It's Surprising to People
          5. Issue 3: It's Surprising to Compilers
          6. Aside: It's the Namespace That's Confusing Them
          7. Two Non-Workarounds
          8. Summary
      9. 9. Export Restrictions, Part 1: Fundamentals
        1. Solution
        2. A Tale of Two Models
        3. Illustrating the Issues
        4. Export InAction [sic]
        5. Issue the First: Source Exposure
        6. Issue the Second: Dependencies and Build Times
        7. Summary
      10. 10. Export Restrictions, Part 2: Interactions, Usability Issues, and Guidelines
        1. Solution
          1. Historical Perspective: 1988-1996
          2. 1996
          3. Our Export Experience to Date
          4. Ch-ch-ch-changes: Export's Shadow Falls on Existing Language Features
          5. Export Can Be Difficult to Use Correctly
          6. Potential Benefits of Export
          7. Morals
    6. Exception Safety Issues and Techniques
      1. 11. Try and Catch Me
        1. Solution
          1. Playing catch
          2. There's More to Life Than Playing catch
          3. Summary
      2. 12. Exception Safety: Is It Worth It?
        1. Solution
          1. The Abrahams Guarantees
          2. When Are Stronger Guarantees Worthwhile?
      3. 13. A Pragmatic Look at Exception Specifications
        1. Solution
          1. Moving Violations
          2. The Story So Far
          3. Issue the First: A “Shadow Type System”
          4. Issue the Second: (Mis)understandings
          5. The Scoop on Exception Specifications
          6. Summary
    7. Class Design, Inheritance, and Polymorphism
      1. 14. Order, Order!
        1. Solution
          1. Summary: A(nother) Word About Inheritance
      2. 15. Uses and Abuses of Access Rights
        1. Solution
          1. Criminal #1: The Forger
          2. Criminal #2: The Pickpocket
          3. Criminal #3: The Cheat
          4. Person #4: The Language Lawyer
          5. Don't Subvert
      3. 16. (Mostly) Private
        1. Solution
          1. The Basic Story: Accessibility
          2. The Other Story: Visibility
          3. Back to the First Story: Granting Access
          4. Summary
      4. 17. Encapsulation
        1. Solution
          1. Encapsulation's Place in OO
          2. Public, Protected, or Private Data?
          3. A General Transformation
          4. A Case in Point
          5. Summary
      5. 18. Virtuality
        1. Solution
          1. The Common Advice About Base Class Destructors
          2. Virtual Question #1: Publicity vs. Privacy?
          3. Virtual Question #2: What About Base Class Destructors?
          4. Summary
      6. 19. Enforcing Rules for Derived Classes
        1. Solution
          1. Implicitly Generated Functions (or, What the Compiler Does for/to You)
          2. Exception Specifications of Implicitly Declared Functions
          3. Implicit Default Constructor
          4. Implicit Copy Constructor
          5. Implicit Copy Assignment Operator
          6. Implicit Destructor
          7. An auto_ptr Member
          8. Renegade Children and Other Family Problems
          9. Enforcing Rules for Derived Classes
          10. Summary
    8. Memory and Resource Management
      1. 20. Containers in Memory, Part 1: Levels of Memory Management
        1. Solution
          1. Memory Managers and Their Strategies: A Brief Survey
          2. Plotting Strategy
          3. Summary
      2. 21. Containers in Memory, Part 2: How Big Is It Really?
        1. Solution
          1. “I'll Take 'Operator New' for 200 Bytes, Alex”
          2. Memory and the Standard Containers: The Basic Story
          3. Memory and the Standard Containers: The Real World
          4. Summary
      3. 22. To new, Perchance to throw, Part 1: The Many Faces of new
        1. Solution
        2. In-Place, Plain, and Nothrow new
        3. Class-Specific new
        4. A Name-Hiding Surprise
        5. Summary
      4. 23. To new, Perchance to throw, Part 2: Pragmatic Issues in Memory Management
        1. Solution
          1. Exceptions, Errors, and new(nothrow)
          2. When the Theoretical Rubber Meets the Real-World Road
          3. What Should You Check?
          4. Summary
    9. Optimization and Efficiency
      1. 24. Constant Optimization?
        1. Solution
          1. How Writing const Really Can Optimize
          2. Summary
      2. 25. inline Redux
        1. Solution
          1. Brief Recap: Inlining
          2. Answer A: At Coding Time
          3. Answer B: At Compile Time
          4. Answer C: At Link Time
          5. Answer D: At Application Installation Time
          6. Answer E: At Run Time
          7. Answer F: At Some Other Time
          8. Summary
      3. 26. Data Formats and Efficiency, Part 1: When Compression Is the Name of the Game
        1. Solution
          1. Different Ways to Represent Data
      4. 27. Data Formats and Efficiency, Part 2: (Even Less) Bit-Twiddling
        1. Solution
          1. BitBuffer, the Binary Slayer
          2. Attempt #1: Bit-Fiddling into an unsigned char Buffer
          3. Attempt #2: Reusing a Standard Bit-Packed Container
          4. The Big Squeeze
          5. Summary
    10. Traps, Pitfalls, and Puzzlers
      1. 28. Keywords That Aren't (or, Comments by Another Name)
        1. Solution
          1. Why Have Keywords?
          2. Our Rather Reserved Cast: The Keywords
          3. Keywords: The Lesser Ones
          4. auto
          5. register
          6. Summary
      2. 29. Is It Initialization?
        1. Solution
          1. Basic Population Mechanics
          2. Interlude: Population Explosion
          3. Proper Population Control
          4. Summary
      3. 30. double or Nothing
        1. Solution
          1. float and double in a Nutshell
          2. The Wheel of Time
          3. A Word About Narrowing Conversions
          4. Summary
      4. 31. Amok Code
        1. Solution
          1. Motivation
          2. Macros Don't Care
          3. Summary
      5. 32. Slight Typos? Graphic Language and Other Curiosities
        1. Solution
      6. 33. Operators, Operators Everywhere
        1. Solution
          1. Who Is Max Munch, and What's He Doing in My C++ Compiler?
          2. Some Fun with Operators
          3. Abuses of Operators
          4. Bonus Challenge Question
          5. Summary
    11. Style Case Studies
      1. 34. Index Tables
        1. Solution
          1. Clarity: A Short Sermon
          2. Dissecting Index Tables
          3. Correcting Mechanical Errors
          4. Improving Style
          5. Summary
      2. 35. Generic Callbacks
        1. Solution
          1. Generic Quality
          2. Dissecting Generic Callbacks
          3. Improving Style
          4. Correcting Mechanical Errors and Limitations
          5. Summary
      3. 36. Construction Unions
        1. Solution
          1. Unions Redux
          2. Dissecting Construction Unions
          3. Into the Code
          4. Underhanded Names
          5. Toward a Better Way: boost::any
          6. Alexandrescu's Discriminated Unions
          7. Summary
      4. 37. Monoliths “Unstrung,” Part 1: A Look at std::string
        1. Solution
          1. Avoid Unduly Monolithic Designs
          2. The Basics of Strings
        2. Summary
      5. 38. Monoliths “Unstrung,” Part 2: Refactoring std::string
        1. Solution
          1. Membership Has Its Rewards—and Its Costs
          2. Operations That Must Be Members
          3. Operations That Should Be Members
          4. Into the Fray: Possibly-Contentious Operations That Can Be Nonmember Nonfriends
      6. 39. Monoliths “Unstrung,” Part 3: std::string Diminishing
        1. Solution
          1. More Operations That Can Be Nonmember Nonfriends
          2. resize
          3. assign and +=/append/push_back
          4. insert
          5. Interlude
      7. 40. Monoliths “Unstrung,” Part 4: std::string Redux
        1. Solution
          1. Still More Operations That Can Be Nonmember Nonfriends
          2. Coffee Break (Sort Of): Erasing erase
          3. Back to Work: Replacing replace
          4. Coffee Break #2: Copying copy and substr
          5. Almost There: Comparing compares
          6. The Home Stretch: Finding the finds
          7. Summary
      8. Bibliography

    Product information

    • Title: Exceptional C++ Style 40 New Engineering Puzzles, Programming Problems, and Solutions
    • Author(s):
    • Release date: August 2004
    • Publisher(s): Addison-Wesley Professional
    • ISBN: 9780201760422