Chris Jefferson has been reading The Daily WTF for a little while now and often wondered how many of the posts here were made up. Then he came across a "don't touch" system; you know, the type so sensitive to changes that even adding too many spaces between compiler switches will cause a complete melt down. The C++ folks with templates experience will definitely appreciate this most ...
// We use this as adding is faster than multiplication // When multiplying small values together template<int firstval, int secondval> struct QuickMultiply { static const int value = QuickMultiply<firstval, secondval - 1>::value + firstval; }; template<int firstval> struct QuickMultiply<firstval,1> { static const int value = firstval; };
This functionish thing is called through the code like this: "QuickMultiply<4,5>::value". Since templates can't accomidate non-constant values, you can only pass in constants. At compile time, the compiler would convert our expression like this ...
result = QuickMultiply<4,5>::value = QuickMultiply<4,4>::value + 4 = QuickMultiply<4,3>::value + 4 + 4 = QuickMultiply<4,2>::value + 4 + 4 + 4 = QuickMultiply<4,1>::value + 4 + 4 + 4 + 4 = 4 + 4 + 4 + 4 + 4 = 20
... for the most part, that is; the fours would actually be wrapped in a struct. It sort of the less efficient way of doing "4*5", which the compiler will also replace with "20" at compile time.