Use of CeePlusPlus templates to execute code at CompileTime.
SmugLispWeenies will no doubt view this as an inefficient reinvention of LispMacros. They are correct of course, templates aren't nearly as powerful as macros, and I'm not a LispWeenie?.
See http://osl.iu.edu/~tveldhui/papers/Template-Metaprograms/meta-art.html
KrzysztofCzarnecki? and UlrichEisenecker?'s GenerativeProgrammingBook is a wonderful tour of the world of generative programming. Much of it is devoted to demonstrating the power of the CeePlusPlus template mechanism. They observe that it is a TuringComplete language that allows a C++ program to manipulate itself at compile time. They go on to show how to build the elements of structured programming (if, switch, while, for) and a lisp processor (cons, car, cdr, ...).
The C++ StandardTemplateLibrary makes only very primitive use of the template mechanism. Programmers must specify their desires by explicitly choosing an implementation, rather than by describing their intent. Czarnecki and Eisenecker show how you can use templates to implement a DomainSpecificLanguage for a client's use: the template meta-program then constructs the appropriate implementation classes based on the preferences.
Another area they explore is optimization. They have been involved in the development of the MatrixTemplateLibrary (MTL). Using templates to define LazyEvaluation at compile time, they show how a matrix expression such as (a+b)*(c+d) can be optimized at compile-time by a suitable Template Metaprogram: the expression is compiled into expression objects which are optimized (re-structured) by the templates. The restructured expression objects are evaluated when the result is assigned. (See ExpressionTemplate.)
There's much more to the book than C++ templates: they discuss a range of methods and languages for generative software (I found the first couple of chapters a bit tedious). They make a strong case for the limitations of today's component technologies. They give a thorough review of Microsoft's IntentionalProgramming research http://www.research.microsoft.com/IP). But their practical examples are written in C++. I had thought I understood templates: now I know how much I had missed.
--DaveWhipp.
BlitzPlusPlus uses a lot of template metaprograms. The PatternTemplateLibrary? also has some interesting, though convoluted, use of parameterization.
DaixtroseLib is a CeePlusPlus header library that retakes ideas developed by ToddVeldhuizen? and GeoffreyFurnish?. DaixtroseLib is ExpressionTemplates for everyone. With the help of this library the ExpressionTemplate technique now is available for everyone. DaixtroseLib takes the task of delaying the evaluation and building the expressions, such that users only have to provide the appropriate evaluation methods and by this can plug any existing class into the ExpressionTemplate context. The library offers fine-grained control over disambiguation rules and copy/reference semantics and was designed to have other libraries built on top of it. --MarkusWerle
See http://arxiv.org/html/cs.CL/0104010
Other books: CppTemplatesTheCompleteGuide by DavidVandevoorde? and NicolaiJosuttis?, ModernCeePlusPlusDesign, CppTemplateMetaprogramming, which describes BoostMpl (MPL).
CategoryCpp CategoryCppTemplates CategoryMetaprogramming CategoryLanguageFeature TemplateMetaprogrammingTechniques