Imperfect C++ : Practical Solutions for Real-Life Programming
C++, although a marvelous language, isn't perfect. Matthew Wilson has been working with it for over a decade, and during that time he has found inherent limitations that require skillful workarounds. In this book, he doesn't just tell you what's wrong with C++, but offers practical techniques and tools for writing code that's more robust, flexible, efficient, and maintainable. He shows you how to tame C++'s complexity, cut through its vast array of paradigms, take back control over your code—and get far better results.
If you're a long-time C++ developer, this book will help you see your programming challenges in new ways—and illuminate powerful techniques you may never have tried. If you're newer to C++, you'll learn principles that will make you more effective in all of your projects. Along the way, you'll learn how to:
Overcome deficiencies in C++'s type system
Enforce software design through constraints, contracts, and assertions
Handle behavior ignored by the standard— including issues related to dynamic libraries, static objects, and threading
Achieve binary compatibility between dynamically loading components
Understand the costs and disadvantages of implicit conversions—and the alternatives
Increase compatibility with diverse compilers, libraries, and operating environments
Help your compiler detect more errors and work more effectively
Understand the aspects of style that impact reliability
Apply the Resource Acquisition Is Initialization mechanism to a wide variety of problem domains
Manage the sometimes arcane relationship between arrays and pointers
Use template programming to improve flexibility and robustness
Extend C++: including fast string concatenation, a true NULL-pointer, flexible memory buffers, Properties, multidimensional arrays, and Ranges
The CD-ROM contains a valuable variety of C++ compilers, libraries, test programs, tools, and utilities, as well as the author's related journal articles. New and updated imperfections, along with software libraries and example code are available online at http://imperfectcplusplus.com.
Table of Contents:
1. Enforcing Design: Constraints, Contracts, and Assertions.
Eggs and Ham.
Compile-Time Contracts: Constraints.
Runtime Contracts: Preconditions, Postconditions, and Invariants.
2. Object Lifetime.
The Object Life Cycle.
Controlling Your Clients.
MILs and Boon.
3. Resource Encapsulation.
A Taxonomy of Resource Encapsulation.
4. Data Encapsulation and Value Types.
A Taxonomy of Data Encapsulation.
Value Types and Entity Types.
A Taxonomy of Value Types.
Arithmetic Value Types.
Value Types Coda.
5. Object Access Models.
Copied for Caller.
Given to Caller.
6. Scoping Classes.
APIs and Services.
II. SURVIVING THE REAL WORLD.
C ABI Requirements.
C++ ABI Requirements.
I Can C Clearly Now.
8. Objects Across Borders.
Mostly Portable vtables?
9. Dynamic Libraries.
Calling Functions Explicitly.
Indentity: Link Units and Link Space.
Dynamic Libraries: Coda.
Synchronizing Integer Access.
Synchronizing Block Access: Critical Regions.
Atomic Integer Performance.
Thread Specific Storage.
Nonlocal Static Objects: Globals.
Function-Local Static Objects.
Return Value Optimization.
Empty Base Optimization.
Empty Derived Optimization.
III. LANGUAGE CONCERNS.
13. Fundamental Types.
May I Have a byte?
Fixed-Sized Integer Types.
Large Integer Types.
14. Arrays and Pointers.
Don't Repeat Yourself.
Arrays Decay into Pointers.
Cannot Pass Arrays to Functions.
Arrays Are Always Passed by Address.
Arrays of Inherited Types.
Cannot Have Multidimensional Arrays.
NULL-The Keyword That Wasn't.
Down to Zero.
Bending the Truth.
What's in a Definition?
The Good, the Bad, and the Ugly.
IV. COGNIZANT CONVERSIONS.
Casting in C++.
The Case for C Casts.
Casts on Steroids.
Embracing Change and Enhancing Flexibility.
Composite Shim Concepts.
Namespaces and Koenig Lookup.
Why Not Traits?
Breaking Up the Monolith.
Binding Data and Operations.
Rubbing Up to Concepts.
Simulated Compile-Time Polymorphism: Reverse Bolt-ins.
Parameterized Polymorphic Packaging.
23. Template Constructors.
Template Constructor Specialization.
Template Constructors: Coda.
24. operator bool().
operator int() const.
operator void () const.
operator bool() const.
operator boolean const () const.
operator int boolean::() const.
Operating in the Real World.
25. Fast, Non-intrusive String Concatenation.
Working with Other String Classes.
26. What's Your Address?
Can't Get the Real Address.
What Actions Are Carried Out during Conversion?
What Do We Return?
What's Your Address: Coda.
27. Subscript Operators.
Pointer Conversion versus Subscript Operators.
28. Increment Operators.
Missing Postfix Operators.
29. Arithmetic Types.
Initialization (Value Construction).
Accessing the Value.
Truncations, Promotions, and Tests.
Arithmetic Types: Coda.
VI. EXTENDING C++.
31. Return Value Lifetime.
A Taxonomy of Return Value Lifetime Gotchas.
Solution 3-Extending RVL.
Solution 4-Static Array Size Determination.
Solution 5-Conversion Shims.
RVL: The Big Win for Garbage Collection.
Return Value Lifetime: Coda.
A Taxonomy of Memory.
The Best of Both Worlds.
33. Multidimensional Arrays.
Facilitating Subscript Syntax.
Sized at Run Time.
Sized at Compile Time.
Multidimensional Arrays: Coda.
34. Functors and Ranges.
Functors and Ranges: Coda.
Appendix A. Compilers and Libraries.
Appendix B. "Watch That Hubris!"
DRY Rued Yesterday.
To Insanity, and Beyond!
Appendix C. Arturius.
Appendix D. The CD.