Wednesday, June 6, 2007

More Monkey^wTemplate Magic

From Bjarn's paper here.

Same idea as before, but perhaps more clear.

template <bool b, class X, class Y>
struct if_ {typedef X type; };

template <class X, class Y>
struct if_ <false,x,y> { typedef Y type; };

void f() { if_<sizeof(foobar)<40,Foo,Bar>::type xy; }

Apparently, template programming is Turing complete.

No comments: