Home > Articles

  • Print
  • + Share This
This chapter is from the book

23.4 Computational Completeness

Our Sqrt<> example demonstrates that a template metaprogram can contain:

  • State variables: The template parameters

  • Loop constructs: Through recursion

  • Execution path selection: By using conditional expressions or specializations

  • Integer arithmetic

If there are no limits to the amount of recursive instantiations and the number of state variables that are allowed, it can be shown that this is sufficient to compute anything that is computable. However, it may not be convenient to do so using templates. Furthermore, because template instantiation requires substantial compiler resources, extensive recursive instantiation quickly slows down a compiler or even exhausts the resources available. The C++ standard recommends but does not mandate that 1024 levels of recursive instantiations be allowed as a minimum, which is sufficient for most (but certainly not all) template metaprogramming tasks.

Hence, in practice, template metaprograms should be used sparingly. There are a few situations, however, when they are irreplaceable as a tool to implement convenient templates. In particular, they can sometimes be hidden in the innards of more conventional templates to squeeze more performance out of critical algorithm implementations.

  • + Share This
  • 🔖 Save To Your Account