// Booleans template struct bool_t { static const bool value = x; }; typedef bool_t false_t; typedef bool_t true_t; false_t false_; true_t true_; struct if_t { template A operator()(true_t, A, B) const; template B operator()(false_t, A, B) const; } if_; struct or_t { template bool_t operator()(false_t, bool_t) const; template true_t operator()(true_t, bool_t) const; } or_; struct and_t { template bool_t operator()(true_t, bool_t) const; template false_t operator()(false_t, bool_t) const; } and_; // Integers template struct int_t { int_t operator()(...); static const int value = n; }; int_t<0> zero; int_t<1> one; int_t<2> two; int_t<3> three; int_t<4> four; struct add_t { template int_t operator()(int_t, int_t) const; } add; struct times_t { template int_t operator()(int_t, int_t) const; } times; struct divide_t { template int_t operator()(int_t, int_t) const; } divide; struct dec_t { template int_t operator()(int_t) const; } dec; struct equal_t { template true_t operator()(int_t, int_t) const; template false_t operator()(int_t, int_t) const; } equal; // Placeholders template struct placeholder; template <> struct placeholder<1> { template T operator()(T, ...) const; }; template <> struct placeholder<2> { template T2 operator()(T1, T2, ...) const; }; placeholder<1> _1; placeholder<2> _2; // Bind implementation. template Op identity(Op); template struct apply1 { typedef typeof(identity(Op())(T1())) type; }; template struct apply2 { typedef typeof(identity(Op())(T1(), T2())) type; }; template struct bind1_impl { template typename apply1::type >::type operator()(T1) const; template typename apply1::type >::type operator()(T1) const; }; template struct bind2_impl { template typename apply2::type, typename apply1::type >::type operator()(T1) const; template typename apply2::type, typename apply2::type >::type operator()(T1) const; }; struct bind_t { template bind1_impl operator()(Op, B1) const; template bind2_impl operator()(Op, B1, B2) const; } bind; // Factorial struct factorial_t { static factorial_t instance; template typeof(if_(equal(zero, T()), one, bind(times, _1, bind(instance, bind(dec, _1)))))(T()) operator()(T) const; } factorial; #include int main() { typedef typeof(factorial(int_t<10>())) result; std::cout<<"10! = "<