Friday, July 31, 2009

Template Hell

Man, When I first saw pointers, referencing and de-referencing and all, I thought that I had seen the worst that C++ can throw at you. And now come and greet the new enemy that I found

TEMPLATES!!!

So you think you know what templates do ? Lets see, they are a type of metaprogramming right ? They allow you to use C++ compiler as an interpreter and write generic code with the use of which C++ compiler would generate specific code. So that is what templates does ? Nope. Sorry but I thought so too. But along comes this code snippet which supposedly implements a while loop. Go figure! And when you do please tell me too :D

template
class loop {
private: enum { go = (I-1) != 0 };
public: static inline void f() {
// Whatever needs to go here
loop::f(); }
};

class loop<0> {
public:
static inline void f()
{ }
};

loop::f();

No comments: