C++ Templates – Is ‘C++ Templates: The Complete Guide’ Up to Date with C++11?

ctemplates

The book C++ Templates: The Complete Guide, (c) 2002, looks very appealing to me, but as it's 12 years old, I'm concerned it may be out of date. There are some highly favorable Amazon reviews dated as recently as a year ago, and I don't see any other very recent titles on C++ templates. Does anyone know whether a book about templates published in 2002 (as opposed to a book specifically about the STL) would have syntax or concepts that have been superseded or added to by, say, C++11?

The following is from the Amazon book description:

Templates are among the most powerful features of C++, but they are
too often neglected, misunderstood, and misused. C++ Templates: The
Complete Guide provides software architects and engineers with a clear
understanding of why, when, and how to use templates to build and
maintain cleaner, faster, and smarter software more efficiently.

C++ Templates begins with an insightful tutorial on basic concepts and
language features. The remainder of the book serves as a comprehensive
reference, focusing first on language details, then on a wide range of
coding techniques, and finally on advanced applications for templates.
Examples used throughout the book illustrate abstract concepts and
demonstrate best practices.

Readers learn

  • The exact behaviors of templates
  • How to avoid the pitfalls associated with templates
  • Idioms and techniques, from the basic to the previously undocumented
  • How to reuse source code without threatening performance or safety
  • How to increase the efficiency of C++ programs
  • How to produce more flexible and maintainable software

Best Answer

Updated (2016/08/24)

Recommended article with focus on C++11:

An introduction to C++'s SFINAE concept: compile-time introspection of a class member (Jean Guegant)


As a learner of C++ Template Metaprogramming a few months ago, I would still recommend to read this book.

It does not include C++11, but it lays out the syntax and the C++ specifications on how a compiler should process template code. The book covers a lot of less known corners which will become crucial if you need to use C++ templates substantially, whether or not metaprogramming is involved.


The missing parts from C++11 are:


There is a construct which you will see a lot in code that is pasted onto Stackoverflow: std::enable_if. However, in practice, I find this quite difficult to use, because of the One-Definition Rule and SFINAE. This is why this book is important, because it goes over the basics.


If you use C++ templates substantially in a cross-platform library, you will need to test-compile your code in different compilers. This is because each compiler has different level of compliance to the C++ specification. Code that compiles fine on one may be reject by the other, and occasionally the compiler that accepts it is the one that breaks the specification.


Here is my reading notes when I go through this book. Hope it will be helpful.

Book reading notes - C++ Templates - The Complete Guide (2003)

  • Ch 2.5 - Function Templates - Summary
    • Overload resolution, and best practices
  • Ch 3.3 - Specialization of Class Templates
  • Ch 3.4 - Partial Specialization
    • Ambiguity (ODR error), and how to resolve it. ODR = One-Definition Rule
  • Ch 4.3 - Restrictions for Non-type Template Parameters
    • Example: string literals with external linkage
  • Ch 5 and Ch 6 - These two chapters are very depressing. You may want to skip it and then go back when you are emotionally ready for using templates.

  • Part II (Ch 8 - Ch ???) The section of advanced stuff.

  • Ch 8 - More rules

    • For a programmer already familiar with the entire Ch 3 (class templates - basics), Ch 8.1 and 8.2 should be read immediately following Ch 3, because the additional rules are just as important and relevant as the basic rules. This will save some pain.
  • Ch 8.3.1 (Page 106 in 12th Printing) mentions SFINAE for the first time.

  • Ch 8.3 - This section introduces a lot of circumstances where template argument substitution would have led to nonsensical syntactic expansions (and thus become invalid, which in return necessitates SFINAE). This chapter has a steep learning curve even for the experienced C++ programmers.
  • Ch 11 - Template Argument Deduction

    • Ch11 contains some contrived Template Argument Deduction examples that are necessary for understanding some of the equally contrived SFINAE applications.
  • Ch 12.4 onward - Partial class template specialization.

    • This covers the internal mechanics that is often called upon in an SFINAE application.

Part III (Ch 17 - Ch ???) Metaprogramming.

  • Ch 17 - Begins with introductory functional programming.

  • App. A.3 - One-Definition Rule (ODR) in detail.

Related Topic