Modern C++ Design: Generic Programming and Design Patterns Applied

Book description

Modern C++ Designis an important book. Fundamentally, it demonstrates ‘generic patterns’ or ‘pattern templates’ as a powerful new way of creating extensible designs in C++–a new way to combine templates and patterns that you may never have dreamt was possible, but is. If your work involves C++ design and coding, you should read this book. Highly recommended.
–Herb Sutter

What’s left to say about C++ that hasn’t already been said? Plenty, it turns out.
–From the Foreword by John Vlissides

In Modern C++ Design, Andrei Alexandrescu opens new vistas for C++ programmers. Displaying extraordinary creativity and programming virtuosity, Alexandrescu offers a cutting-edge approach to design that unites design patterns, generic programming, and C++, enabling programmers to achieve expressive, flexible, and highly reusable code.

This book introduces the concept of generic components–reusable design templates that produce boilerplate code for compiler consumption–all within C++. Generic components enable an easier and more seamless transition from design to application code, generate code that better expresses the original design intention, and support the reuse of design structures with minimal recoding.

The author describes the specific C++ techniques and features that are used in building generic components and goes on to implement industrial strength generic components for real-world applications. Recurring issues that C++ developers face in their day-to-day activity are discussed in depth and implemented in a generic way. These include:

  • Policy-based design for flexibility

  • Partial template specialization

  • Typelists–powerful type manipulation structures

  • Patterns such as Visitor, Singleton, Command, and Factories

  • Multi-method engines

  • For each generic component, the book presents the fundamental problems and design options, and finally implements a generic solution.

    In addition, an accompanying Web site, http://www.awl.com/cseng/titles/0-201-70431-5, makes the code implementations available for the generic components in the book and provides a free, downloadable C++ library, called Loki, created by the author. Loki provides out-of-the-box functionality for virtually any C++ project.

    Get a value-added service! Try out all the examples from this book at www.codesaw.com. CodeSaw is a free online learning tool that allows you to experiment with live code from your book right in your browser.



    0201704315B11102003

    Table of contents

    1. Title Page
    2. Copyright Page
    3. The C++ In-Depth Series
      1. Titles in the Series
    4. Contents
    5. Foreword by Scott Meyers
    6. Foreword by John Vlissides
    7. Preface
      1. Audience
      2. Loki
      3. Organization
    8. Acknowledgments
    9. Part I: Techniques
      1. 1. Policy-Based Class Design
        1. 1.1. The Multiplicity of Software Design
        2. 1.2. The Failure of the Do-It-All Interface
        3. 1.3. Multiple Inheritance to the Rescue?
        4. 1.4. The Benefit of Templates
        5. 1.5. Policies and Policy Classes
        6. 1.6. Enriched Policies
        7. 1.7. Destructors of Policy Classes
        8. 1.8. Optional Functionality Through Incomplete Instantiation
        9. 1.9. Combining Policy Classes
        10. 1.10. Customizing Structure with Policy Classes
        11. 1.11. Compatible and Incompatible Policies
        12. 1.12. Decomposing a Class into Policies
        13. 1.13. Summary
      2. 2. Techniques
        1. 2.1. Compile-Time Assertions
        2. 2.2. Partial Template Specialization
        3. 2.3. Local Classes
        4. 2.4. Mapping Integral Constants to Types
        5. 2.5. Type-to-Type Mapping
        6. 2.6. Type Selection
        7. 2.7. Detecting Convertibility and Inheritance at Compile Time
        8. 2.8. A Wrapper Around type_info
        9. 2.9. NullType and EmptyType
        10. 2.10. Type Traits
        11. 2.11. Summary
      3. 3. Typelists
        1. 3.1. The Need for Typelists
        2. 3.2. Defining Typelists
        3. 3.3. Linearizing Typelist Creation
        4. 3.4. Calculating Length
        5. 3.5. Intermezzo
        6. 3.6. Indexed Access
        7. 3.7. Searching Typelists
        8. 3.8. Appending to Typelists
        9. 3.9. Erasing a Type from a Typelist
        10. 3.10. Erasing Duplicates
        11. 3.11. Replacing an Element in a Typelist
        12. 3.12. Partially Ordering Typelists
        13. 3.13. Class Generation with Typelists
        14. 3.14. Summary
        15. 3.15. Typelist Quick Facts
      4. 4. Small-Object Allocation
        1. 4.1. The Default Free Store Allocator
        2. 4.2. The Workings of a Memory Allocator
        3. 4.3. A Small-Object Allocator
        4. 4.4. Chunks
        5. 4.5. The Fixed-Size Allocator
        6. 4.6. The SmallObjAllocator Class
        7. 4.7. A Hat Trick
        8. 4.8. Simple, Complicated, Yet Simple in the End
        9. 4.9. Administrivia
        10. 4.10. Summary
        11. 4.11. Small-Object Allocator Quick Facts
    10. Part II: Components
      1. 5. Generalized Functors
        1. 5.1. The Command Design Pattern
        2. 5.2. Command in the Real World
        3. 5.3. C++ Callable Entities
        4. 5.4. The Functor Class Template Skeleton
        5. 5.5. Implementing the Forwarding Functor::operator()
        6. 5.6. Handling Functors
        7. 5.7. Build One, Get One Free
        8. 5.8. Argument and Return Type Conversions
        9. 5.9. Handling Pointers to Member Functions
        10. 5.10. Binding
        11. 5.11. Chaining Requests
        12. 5.12. Real-World Issues I: The Cost of Forwarding Functions
        13. 5.13. Real-World Issues II: Heap Allocation
        14. 5.14. Implementing Undo and Redo with Functor
        15. 5.15. Summary
        16. 5.16. Functor Quick Facts
      2. 6. Implementing Singletons
        1. 6.1. Static Data + Static Functions != Singleton
        2. 6.2. The Basic C++ Idioms Supporting Singletons
        3. 6.3. Enforcing the Singleton’s Uniqueness
        4. 6.4. Destroying the Singleton
        5. 6.5. The Dead Reference Problem
        6. 6.6. Addressing the Dead Reference Problem (I): The Phoenix Singleton
        7. 6.7. Addressing the Dead Reference Problem (II): Singletons with Longevity
        8. 6.8. Implementing Singletons with Longevity
        9. 6.9. Living in a Multithreaded World
        10. 6.10. Putting It All Together
        11. 6.11. Working with SingletonHolder
        12. 6.12. Summary
        13. 6.13. SingletonHolder Class Template Quick Facts
      3. 7. Smart Pointers
        1. 7.1. Smart Pointers 101
        2. 7.2. The Deal
        3. 7.3. Storage of Smart Pointers
        4. 7.4. Smart Pointer Member Functions
        5. 7.5. Ownership-Handling Strategies
        6. 7.6. The Address-of Operator
        7. 7.7. Implicit Conversion to Raw Pointer Types
        8. 7.8. Equality and Inequality
        9. 7.9. Ordering Comparisons
        10. 7.10. Checking and Error Reporting
        11. 7.11. Smart Pointers to const and const Smart Pointers
        12. 7.12. Arrays
        13. 7.13. Smart Pointers and Multithreading
        14. 7.14. Putting It All Together
        15. 7.15. Summary
        16. 7.16. SmartPtr Quick Facts
      4. 8. Object Factories
        1. 8.1. The Need for Object Factories
        2. 8.2. Object Factories in C++: Classes and Objects
        3. 8.3. Implementing an Object Factory
        4. 8.4. Type Identifiers
        5. 8.5. Generalization
        6. 8.6. Minutiae
        7. 8.7. Clone Factories
        8. 8.8. Using Object Factories with Other Generic Components
        9. 8.9. Summary
        10. 8.10. Factory Class Template Quick Facts
        11. 8.11. CloneFactory Class Template Quick Facts
      5. 9. Abstract Factory
        1. 9.1. The Architectural Role of Abstract Factory
        2. 9.2. A Generic Abstract Factory Interface
        3. 9.3. Implementing AbstractFactory
        4. 9.4. A Prototype-Based Abstract Factory Implementation
        5. 9.5. Summary
        6. 9.6. AbstractFactory and ConcreteFactory Quick Facts
      6. 10. Visitor
        1. 10.1. Visitor Basics
        2. 10.2. Overloading and the Catch-All Function
        3. 10.3. An Implementation Refinement: The Acyclic Visitor
        4. 10.4. A Generic Implementation of Visitor
        5. 10.5. Back to the “Cyclic” Visitor
        6. 10.6. Hooking Variations
        7. 10.7. Summary
        8. 10.8. Visitor Generic Components Quick Facts
      7. 11. Multimethods
        1. 11.1. What Are Multimethods?
        2. 11.2. When Are Multimethods Needed?
        3. 11.3. Double Switch-on-Type: Brute Force
        4. 11.4. The Brute-Force Approach Automated
        5. 11.5. Symmetry with the Brute-Force Dispatcher
        6. 11.6. The Logarithmic Double Dispatcher
        7. 11.7. FnDispatcher and Symmetry
        8. 11.8. Double Dispatch to Functors
        9. 11.9. Converting Arguments: static_cast or dynamic_cast?
        10. 11.10. Constant-Time Multimethods: Raw Speed
        11. 11.11. BasicDispatcher and BasicFastDispatcher as Policies
        12. 11.12. Looking Forward
        13. 11.13. Summary
        14. 11.14. Double Dispatcher Quick Facts
    11. Appendix. A Minimalist Multithreading Library
      1. A.1. A Critique of Multithreading
      2. A.2. Loki’s Approach
      3. A.3. Atomic Operations on Integral Types
      4. A.4. Mutexes
      5. A.5. Locking Semantics in Object-Oriented Programming
      6. A.6. Optional volatile Modifier
      7. A.7. Semaphores, Events, and Other Good Things
      8. A.8. Summary
    12. Bibliography
    13. Index

    Product information

    • Title: Modern C++ Design: Generic Programming and Design Patterns Applied
    • Author(s): Andrei Alexandrescu
    • Release date: February 2001
    • Publisher(s): Addison-Wesley Professional
    • ISBN: None