- 1. Templates and Generic Programming
- 1.1. Understand implicit interfaces and compile-time polymorphism
- 1.2. Understand the two meanings of typename
- 1.3. Know how to access names in templatized base classes
- 1.4. Factor parameter-independent code out of templates
- 1.5. Use member function templates to accept “all compatible types”
- 1.6. Define non-member functions inside templates when type conversions are desired
- 1.7. Use traits classes for information about types
- 1.8. Be aware of templatee metaprogramming
(I read a Chinese version of the book, any translation problem plz point out.
- classes and templates both support interfaces and polymorphism.
- as for classes, interfaces is explicit, based on function signatures,
and their polymorphism happen in run-time via virtual functions.
- as for templates, interfaces is implicit, based on valid expressions,
and their polymorphism happen in compile-time via template instantiation and function overloading resolution.
dependent names: the names appeared in template and dependent on some template parameter.
nested dependent names: the dependent name nested in class.
C::const_iterator is a nested dependent type name.
C::const_iterator* x, what if
const_iteratoris a static member variable and
xis a global variable?
typenameare the same when declare template parameters.
typenameto indentify the nested dependent name,
but it mustn’t modify the base class in base class lists and member initialization lists.
this->to refer to the member names of base class templates in derived class templates.
- use base class modifier,
- templates can generate a couple of classes and functions,
so any template code should not be dependent on some template parameter which can cause code bloat.
- code bloat caused by non-type template parameters can be removed via replacing them with
function parameter or using class member variable.
- code bload caused by type parameters can be reduced via sharing the implementation
when the instantiation types are with completely same binary representations,
such as strongly typed pointers
(T*)to untyped pointers
- use member function templates to generate the functions which accept “all compatible types”.
- you need to declare normal copy constructor and copy assignment operator when using
member function templates. or the compiler will generate one which may be not what
- traits classes make “type-related information” be available in compile-time,
which is implemented by templates and template specializations.
- traits classes can execute the
if...elsetest in compile-time via overloading.
- TMP is Turing-complete, and TMP loops is recursive template instantiation.
- TMP can be used:
- validate type informations or some others.
- optimize matrix operations, such as expression templates.
- generate custom design patterns for users. (TMP-based policy-based design) -> generative programming