You are previewing Advanced Metaprogramming in Classic C++.
O'Reilly logo
Advanced Metaprogramming in Classic C++

Book Description

Advanced Metaprogramming in Classic C++ aims to be both an introduction and a reference to C++ template metaprogramming (TMP); TMP is presented in the book as a set of techniques that will bring a new style in C++ and make code exceptionally clear and efficient. The book deals with language aspects, design patterns, examples and applications (seen as case studies). Special emphasis is put on small reusable techniques that will improve the quality of daily work.

What makes the book exceptional is the level of understanding of the concepts involved imparted by the author. This is not just a rote overview of metaprogramming. You will truly understand difficult topics like static assertions, how to write metafunctions, overload resolution, lambda expressions, and many others. More than that, you will work through them with practical examples guided by the author's frank explanations. This book requires you to think and to learn and to understand the language so that you can program at a higher level.

Table of Contents

  1. Cover
  2. Title
  3. Copyright
  4. Dedication 1
  5. Dedication 2
  6. Contents at a Glance
  7. Contents
  8. About the Author
  9. About the Technical Reviewer
  10. Acknowledgments
  11. Preface
  12. Part I: #include <prerequisites>
    1. Chapter 1: Templates
      1. 1.1. C++ Templates
        1. 1.1.1. Typename
        2. 1.1.2. Angle Brackets
        3. 1.1.3. Universal Constructors
        4. 1.1.4. Function Types and Function Pointers
        5. 1.1.5. Non-Template Base Classes
        6. 1.1.6. Template Position
      2. 1.2. Specialization and Argument Deduction
        1. 1.2.1. Deduction
        2. 1.2.2. Specializations
        3. 1.2.3. Inner Class Templates
      3. 1.3. Style Conventions
        1. 1.3.1. Comments
        2. 1.3.2. Macros
        3. 1.3.3. Symbols
        4. 1.3.4. Generality
        5. 1.3.5. Template Parameters
        6. 1.3.6. Metafunctions
        7. 1.3.7. Namespaces and Using Declarations
      4. 1.4. Classic Patterns
        1. 1.4.1. size_t and ptrdiff_t
        2. 1.4.2. void T::swap(T&)
        3. 1.4.3. bool T::empty() const; void T::clear()
        4. 1.4.4. X T::get() const; X T::base() const
        5. 1.4.5. X T::property() const; void T::property(X)
        6. 1.4.6. Action(Value); Action(Range)
        7. 1.4.7. Manipulators
        8. 1.4.8. Position of Operators
        9. 1.4.9. Secret Inheritance
        10. 1.4.10. Literal Zero
        11. 1.4.11. Boolean Type
        12. 1.4.12. Default and Value Initialization
      5. 1.5. Code Safety
      6. 1.6. Compiler Assumptions
        1. 1.6.1. Inline
        2. 1.6.2. Error Messages
        3. 1.6.3. Miscellaneous Tips
      7. 1.7. Preprocessor
        1. 1.7.1. Include Guards
        2. 1.7.2. Macro Expansion Rules
    2. Chapter 2: Small Object Toolkit
      1. 2.1. Hollow Types
        1. 2.1.1. instance_of
        2. 2.1.2. Selector
        3. 2.1.3. Static Value
        4. 2.1.4. Size of Constraints
      2. 2.2. Static Assertions
        1. 2.2.1. Boolean Assertions
        2. 2.2.2. Assert Legal
        3. 2.2.3. Assertions with Overloaded Operators
        4. 2.2.4. Modeling Concepts with Function Pointers
        5. 2.2.5. Not Implemented
      3. 2.3. Tagging Techniques
        1. 2.3.1. Type Tags
        2. 2.3.2. Tagging with Functions
        3. 2.3.3. Tag Iteration
        4. 2.3.4. Tags and Inheritance
  13. Part 2: #include <techniques>
    1. Chapter 3: Static Programming
      1. 3.1. Static Programming with the Preprocessor
      2. 3.2. Compilation Complexity
      3. 3.3. Classic Metaprogramming Idioms
        1. 3.3.1. Static Short Circuit
      4. 3.4. Hidden Template Parameters
        1. 3.4.1. Static Recursion on Hidden Parameters
        2. 3.4.2. Accessing the Primary Template
        3. 3.4.3. Disambiguation
      5. 3.5. Traits
        1. 3.5.1. Type Traits
        2. 3.5.2. Type Dismantling
      6. 3.6. Type Containers
        1. 3.6.1. typeat
        2. 3.6.2. Returning an Error
        3. 3.6.3. Depth
        4. 3.6.4. Front and Back
        5. 3.6.5. Find
        6. 3.6.6. Push and Pop
        7. 3.6.7. More on Template Rotation
        8. 3.6.8. Agglomerates
        9. 3.6.9. Conversions
        10. 3.6.10. Metafunctors
      7. 3.7. A Summary of Styles
    2. Chapter 4: Overload Resolution
      1. 4.1. Groups
        1. 4.1.1. From Overload to Groups
        2. 4.1.2. Runtime Decay
      2. 4.2. More Traits
        1. 4.2.1. A Function Set for Strings
        2. 4.2.2. Concept Traits
        3. 4.2.3. Platform-Specific Traits
        4. 4.2.4. Merging Traits
      3. 4.3. SFINAE
        1. 4.3.1. SFINAE Metafunctions
        2. 4.3.2. Multiple Decisions
        3. 4.3.3. Only_If
        4. 4.3.4. SFINAE and Returned Functors
        5. 4.3.5. SFINAE and Software Updates
        6. 4.3.6. Limitations and Workarounds
        7. 4.3.7. SFINAE with Partial Specializations
      4. 4.4. Other Classic Metafunctions with Sizeof
      5. 4.5. Overload on Function Pointers
        1. 4.5.1. Erase
        2. 4.5.2. Swap
        3. 4.5.2. Argument Dominance
    3. Chapter 5: Interfaces
      1. 5.1. Wrapping References
      2. 5.2. Static Interfaces
        1. 5.2.1. Static Interfaces
        2. 5.2.2. Common Errors
        3. 5.2.3. A Static_Interface Implementation
        4. 5.2.4. The Memberspace Problem
        5. 5.2.5. Member Selection
      3. 5.3. Type Hiding
        1. 5.3.1. Trampolines
        2. 5.3.2. Typeinfo Wrapper
        3. 5.3.3. Option_Map
        4. 5.3.4. Option_Parser
        5. 5.3.5. Final Additions
        6. 5.3.6. Boundary Crossing with Trampolines
      4. 5.4. Variant
        1. 5.4.1. Parameter Deletion with Virtual Calls
        2. 5.4.2. Variant with Visitors
      5. 5.5. Wrapping Containers
    4. Chapter 6: Algorithms
      1. 6.1. Algorithm I/O
        1. 6.1.1. Swap-Based or Copy-Based
        2. 6.1.2. Classification of Algorithms
        3. 6.1.3. Iterator Requirements
        4. 6.1.4. An Example: Set Partitioning
        5. 6.1.5. Identifying Iterators
        6. 6.1.6. Selection by Iterator Value Type
      2. 6.2. Generalizations
        1. 6.2.1. Properties and Accessors
        2. 6.2.2. Mimesis
        3. 6.2.3. End of Range
      3. 6.3. Iterator Wrapping
        1. 6.3.1. Iterator Expander
        2. 6.3.2. Fake Pairs
      4. 6.4. Receipts
      5. 6.5. Algebraic Requirements
        1. 6.5.1. Less and NaN
      6. 6.6. The Barton-Nackman Trick
    5. Chapter 7: Code Generators
      1. 7.1. Static Code Generators
      2. 7.2. Double checked Stop
      3. 7.3. Static and Dynamic Hashing
        1. 7.3.1. A Function Set for Characters
        2. 7.3.2. Changing Case
        3. 7.3.3. Mimesis Techniques
        4. 7.3.4. Ambiguous Overloads
        5. 7.3.5. Algorithm I/O
        6. 7.3.6. Mimesis Interface
      4. 7.4. Nth Minimum
      5. 7.5. The Template Factory Pattern
      6. 7.6. Automatic Enumeration of Types
      7. 7.7. If-Less Code
        1. 7.7.1. Smart Constants
        2. 7.7.2. Converting Enum to String
        3. 7.7.3. Self-Modifying Function Tables
    6. Chapter 8: Functors
      1. 8.1. Strong and Weak Functors
      2. 8.2. Functor Composition Tools
      3. 8.3. Inner Template Functors
        1. 8.3.1. Conversion of Functions to Functors
        2. 8.3.2. Conversion of Members to Functors
        3. 8.3.3. More on the Double Wrapper Technique
      4. 8.4. Accumulation
        1. 8.4.1. A Step-by-Step Implementation
      5. 8.5. Drivers
      6. 8.6. Algors
      7. 8.7. Forwarding and Reference Wrappers
    7. Chapter 9: The Opaque Type Principle
      1. 9.1. Polymorphic Results
      2. 9.2. Classic Lambda Expressions
        1. 9.2.1. Elementary Lambda Objects
        2. 9.2.2. Lambda Functions and Operators
        3. 9.2.3. Refinements
        4. 9.2.4. Argument and Result Deduction
        5. 9.2.5. Deducing Argument Type
        6. 9.2.6. Deducing Result Type
        7. 9.2.7. Static Cast
        8. 9.2.8. Arrays
      3. 9.3. Creative Syntax
        1. 9.3.1. Argument Chains with () and []
      4. 9.4. The Growing Object Concept
        1. 9.4.1. String Concatenation
        2. 9.4.2. Mutable Growing Objects
        3. 9.4.3. More Growing Objects
        4. 9.4.4. Chain Destruction
        5. 9.4.5. Variations of the Growing Object
      5. 9.5. Streams
        1. 9.5.1. Custom Manipulators and Stream Insertion
        2. 9.5.2. Range Insertion with a Growing Object
      6. 9.6. Comma Chains
      7. 9.7. Simulating an Infix
  14. Part 3: #include <applications>
    1. Chapter 10: Refactoring
      1. 10.1. Backward Compatibility
      2. 10.2. Refactoring Strategies
        1. 10.2.1. Refactoring with Interfaces
        2. 10.2.2. Refactoring with Trampolines
        3. 10.2.3. Refactoring with Accessors
      3. 10.3. Placeholders
        1. 10.3.1. Switch-Off
        2. 10.3.2. The Ghost
    2. Chapter 11: Debugging Templates
      1. 11.1. Identify Types
        1. 11.1.1. Trapping Types
        2. 11.1.2. Incomplete Types
        3. 11.1.3. Tag Global Variables
      2. 11.2. Integer Computing
        1. 11.2.1. Signed and Unsigned Types
        2. 11.2.2. References to Numeric Constants
      3. 11.3. Common Workarounds
        1. 11.3.1. Debugging SFINAE
        2. 11.3.2. Trampolines
        3. 11.3.3. Compiler Bugs
    3. Chapter 12: C++0x
      1. 12.1. Type Traits
      2. 12.2. Decltype
      3. 12.3. Auto
      4. 12.4. Lambdas
      5. 12.5. Initializers
      6. 12.6. Template Typedefs
      7. 12.7. Extern Template
        1. 12.7.1. Linking Templates
        2. 12.7.2. Extern Template
      8. 12.9. Variadic Templates
    4. Appendix A: Exercises
      1. A.1. Exercises
        1. A.1.1. Extension
        2. A.1.2. Integer
        3. A.1.3. Date Format
        4. A.1.4. Specialization
        5. A.1.5. Bit Counting
        6. A.1.6. Prime Numbers
        7. A.1.7. Typeinfo without RTTI
        8. A.1.8. Hints and Partial Solutions
    5. Appendix B: Bibliography
  15. Index