TITLE: Template metaprograms, or abusing your compiler [ This material taken from "Using C++ Template Metaprograms", by Todd Veldhuizen, C++ Report, May 95, Vol 7, No 4, pp 36-43. ] This shows a quirky, interesting little technique for those who have tight performance requirements requiring such optimizations as loop unrolling. The obtuse use of templates and associated loss of clarity does not make this a useful technique for general programming requirements. It relies on knowing key information at compile time. An example will make this clearer... template class Factorial { public: enum { value = N * Factorial::value }; }; class Factorial<1> { public: enum { value = 1 }; }; The article cited above contains examples for bubblesort and explains how to implement compile-time control flow structures (if/else, switch, loop) and compile-time functions. In addition to the simple example below, the article shows a compile-time sine function and alludes to a compile-time FFT! Consider the example compile-time function below to count the number of set bits in the lower nibble of an integer template class countBits { enum { bit3 = (N & 0x08) ? 1 : 0, bit2 = (N & 0x04) ? 1 : 0, bit1 = (N & 0x02) ? 1 : 0, bit0 = (N & 0x01) ? 1 : 0 }; public: enum { nbits = bit0+bit1+bit2+bit3 }; }; ... int i = countBits<13>::nbits; ...