Thesis (PhD) - Indiana University, Computer Sciences, 2008
Most programming languages do not allow programs to inspect their
static type information or perform computations on it. C++, however,
lets programmers write template metaprograms, which enable programs to
encode static information, perform compile-time computations,
and make static decisions about run-time behavior. Many C++ libraries
and applications use template metaprogramming to build specialized
abstraction mechanisms, implement domain-specific safety checks, and
improve run-time performance.
Template metaprogramming is an emergent capability of the C++ type
system, and the C++ language specification is informal and imprecise.
As a result, template metaprogramming often involves heroic
programming feats and often leads to code that is difficult to read and
maintain. Furthermore, many template-based code generation and
optimization techniques rely on particular compiler implementations,
rather than language semantics, for performance gains.
Motivated by the capabilities and techniques of C++ template
metaprogramming, this thesis documents some common programming patterns,
including static computation, type analysis, generative programming, and the
encoding of domain-specific static checks. It also documents notable
shortcomings to current practice, including limited support for reflection,
semantic ambiguity, and other issues that arise from the pioneering nature of
template metaprogramming. Finally, this thesis presents the design of a
foundational programming language, motivated by the analysis of template
metaprogramming, that allows programs to statically inspect type information,
perform computations, and generate code. The language is specified as a core
calculus and its capabilities are presented in an idealized setting.