Chapter 1 - Function Template
How to define a function template
Template constraints
How to inline a function template
How to instantiate a function template
How to use a template parameter in multiple call parameters
How to define multiple template parameters
How to define a default call argument
Who is the most famous "deducer" in history?
http://www.wikipedia.org/wiki/Deerstalker
Template argument deduction
No implicit type conversions can be performed during template argument deduction
A template parameter that is not used in a call parameter can not be deduced
Rearrange the order of the template parameters
If all the template parameters can be deduced, then the invocation looks like a nontemplate function call
How to use the deduced template argument
How to define a function template default argument
How to define a nontype template parameter
How to overload a function template
Function templates can be overloaded if they differ in their number of call parameters
How to say "Always choose a function template over a function"
How to fully specialize a function template
How to fully specialize a function template with a nontype template parameter
How to define a member function template
Function pointers as template arguments
Lexical cast
References
Lab for Function Template
Chapter 2 - Class Template
How to define a class template
A class template must be defined in a header file (if it wants to be usable by multiple source files)
How to inline a class template member function, version 1
How to inline a class template member function, version 2
How to define a class template nonmember function
How to instantiate a class template
How to define multiple template parameters
How to define a class template default argument
How to define a nontype template parameter
Nontype template parameters can save space, as they do not need to be stored as data members
Nontype template parameters can have default template arguments
How to fully specialize a class template
If all instantiations use fully specialized class templates, then the primary class template does not need to be defined
You can fully specialize a class template that has a nontype template parameter
How to partially specialize a class template
How to partially specialize a class template with multiple template parameters
How to define a member function template
How to define a member function template in a class template
How could this be useful?
Function pointers as template arguments
How to define a template template parameter
How could this be useful?
What if we wanted a container other than vector?
How to publish a function from a class template
How could this be useful?
How to publish a value from a class template
How could this be useful?
Static constexpr data member or enumerator?
How to publish a type from a class template
How could this be useful?
Why do we need to say typename in typename foo::type x;?
How to publish "if (expression) then type1 else type2"
How to inherit from a class template
Inheritance versus templates
Different kinds of polymorphism
References
Lab for Class Template
Chapter 3 - Dimensional Analysis
What is dimensional analsis?
The Goal
The Implementation
References
Lab for Dimensional Analysis
Chapter 4 - Smart Pointer
What is a smart pointer?
How could this be useful?
Resource management
Single owner, scoped, scalar
Single owner, scoped, array
Single owner, non-scoped, scalar
Multiple owners in parallel, scalar
Multiple owners in serial, scalar
Can smart pointers support address arithmetic?
Can smart pointers support comparisons?
Can smart pointers support conversions?
Can smart pointers completely replace dumb pointers?
References
Lab for Smart Pointer
Chapter 5 - Variant Type
Heterogeneous container
Infinite union
Variant type
Variant's interface
Variant's implementation
References
Lab for Variant Type
Chapter 6 - Traits Class
How to ask questions about a type using overloading
How to ask questions about a type using templates
How to ask questions about a type using overloading when the answer is a type
How to ask questions about a type using templates when the answer is a type
Character traits in the Standard
Numeric traits in the Standard
Iterator traits in the Standard
Type traits in the Standard
Predicates about T
Predicates about classes
Predicates about T
Predicates about relationships between types
Predicates about variable number of types
Predicate synonyms
Transformations between types
Transformation synonyms
How can you use template type traits? version 0
How can you use template type traits? version 1
How can you use template type traits? version 2
How can you use template type traits? version 3
How can you use template type traits? version 4
How can you use template type traits? version 5
Implementation techniques - Is T an arithmetic type?
Implementation techniques - Is T an lvalue reference?
Implementation techniques - Is T an array type?
Array traits
Implementation techniques - Is T the same as U?
When substitution failure is an error
When substitution failure is not an error (SFINAE)
Implementation techniques - Does T have typedef type?
Implementation techniques - Does T have member function foo()?
Implementation techniques - Is T convertible to U?
Implementation techniques - Is T a pointer to a member variable?
Implementation techniques - Is T a function type?
Implementation techniques - Is T a pointer to a function type?
Implementation techniques - Is T a pointer to a member function type?
Function traits (class type, arity, result type, argument N type)
Implementation techniques - Is T a struct or class type?
Implementation techniques - Is T a base class of U?
How to test one thing first and another thing second
How to use std::enable_if to choose among function overloads
How to use std::enable_if to choose among partial specializations
When to use SFINAE
How to map from a type to a string
Why didn't I use typeid().name()?
Reflection
References
Lab for Traits Class
Chapter 7 - Policy Class
Combinatorial explosion
How to avoid combinatorial explosion
Policy class
Advantages of policy classes
Disadvantages of policy classes
How to convert object creation into a policy
Another way to delegate behavior at compile-time
References
Lab for Policy Class
Chapter 8 - Type List
What is a type_list?
How to create a type_list
How to output a type_list
How to compute the length of a type_list
How to get the Nth type from a type_list
How to get the index of a type from a type_list
How to append a type_list to a type_list
How to remove one occurrence of a type from a type_list
How to remove all occurrences of a type from a type_list
How to remove duplicates from a type_list
How to replace one occurrence of a type in a type_list
How to replace all occurrences of a type in a type_list
How to reverse a type_list
How to sort the types in a type_list by inheritance
How to use a type_list to sort values in a container
How to multiply inherit from a type_list
References
Lab for Type List
Chapter 9 - Tuple
What is a tuple?
How to define and initialize a tuple
How to query the size of a tuple
How to query the Nth type of a tuple
How to query the Nth value of a tuple
How to unpack a tuple into variables
How to use tie() to swap two variables
How to iterate through a tuple
When to use tuples
References
Lab for Tuple
Chapter 10 - Refactoring Commonality Via Templates
How to enforce that != is always !(==)
What is a shim?
What is a shim class?
How to make a shim class
You can nest shim classes
How to make a shim class that clones
Curiously recurring template pattern (CRTP)
References
Lab
Chapter 11 - Expression Template
How to determine the context of operator[]()
How an array class loses performance
How to defer the evaluation of an expression
How to support expressions of any type
How to support expressions containing literals
How to apply an expression to a value
How to defer computing the + of two arrays
How to combine expression templates and STL vectors
An expression template is a STL container and has STL iterators
A better way to determine the result type of an operator
How can we "automatically" produce this diagram?
References
Lab
Chapter 12 - Template Metaprogramming
Turing-complete language
Factorial as a function
Factorial as a class template
Evaluation is lazy at run-time but not at compile-time
Metafunction
One function can be passed as an argument to another function (via function pointer or functor)
One function can be passed as the result of another function (via function pointer or functor)
Numeric metafunctions
Turing conditional
Turing looping construct
Meta-if statement
Meta-switch statement
Meta-for statement
How to use the induction variable in a meta-for statement
How to bubble-sort an array of integers at run-time
How to bubble-sort an array of integers at compile-time
How to compute prime numbers at compile-time
References
Lab