Compile-time programming with Boost
Templates allow us to write C++ code that is independent of specific types of operands and can thus work unchanged with a large family of types. We can create both function templates and class templates (or struct templates), which take type parameters, nontype parameters (like constant integers), as well as template parameters. When a specialization of a class template is instantiated, member functions that are not directly or indirectly called are never instantiated.
The power of C++ templates goes beyond the ability to write generic code though. C++ templates are a powerful computation subsystem using which we can introspect C++ types, glean their properties, and write sophisticated recursive and branching logic that executes at compile time. Using these capabilities, it is possible to define generic interfaces to implementations that are highly optimized for each type they operate upon.
Basic compile-time control flow using templates
In this section...