This book's aim is to provide a solid foundation on the topic of C++ templates. That foundation is built from
a few different materials:
These materials are introduced by a fairly intuitive tutorial, which makes
the book accessible to moderately novice C++ programmers (though the book as a whole probably better fits the “advanced” category). Some of the appendices also cover non-template topics in more detail than found in most other C++ textbooks (e.g., the topic of overload resolution).
- A detailed explanation of the C++ template language concept itself.
- Common design techniques enabled by C++ templates.
- Illustrative applications (some of are considered “advanced” at the time of writing).
My hope is that this book will help make templates more transparent.
I have observed, for example, that many advanced C++ templates are written using a process of trial and error: A piece of code that is intuitively thought to be workable is fed to a compiler and if it triggers compilation errors, it is repeatedly tweaked until the code is accepted. This procedure is then repeated for a variety of compilers until code is obtained that is portable (in a sense). Though to some degree this may be inevitable by the nature of programming in general, the extent to which it happens with template code does, I believe, negatively affect the quality of that code in general. I suspect that if programmers were confident of what ought to work (and what ought not), their feedback to vendors would quickly improve the quality of their C++ compilers.
Another aspect of C++ templates that I think could gain from additional clarity are the design possibilities they offer (and those they don't offer). For example, I realized after writing this book that certain techniques that had been tossed around C++ committee members in the mid-1990s were being re-discovered several years later. In some cases, experienced programmers had apparently been actively looking for a solution to a problem which was in fact known to some (the possibilities of the SFINAE principle, for example, re-appeared as a novelty in the C++ metaprogramming community). By collecting a fundamental repertoire of basic techniques, along with precise explanations of the language principles that enable them, I'm hoping that other practitioners will more efficiently develop new applications and more advanced idioms.