Listing 9

// Implementation of For<>

struct empty  {   static void exec()  {}   };
// adapts arbitrary types for use with if<>
template<typename T>  struct wrap     {  typedef T result;  };

template < template<typename> class  condition,
          template<typename> class  increment,
          template<typename> class  body,
          typename                 state >
struct For
{
// for compile-time computations. body needs to define result member type 
    typedef typename If<  condition<state>::result,   // step 1
        For< condition,                             // step 1c
            increment,
            body,
            typename increment< typename body<state>::result >::result  
                                                   // steps 1a, 1b
          >,
        wrap< state >
      >::result  result ;
// for loop unrolling. body needs to have a member function exec()
    static void exec() {
      //if condition is true, then execute body & continue to next iteration
       If_exec< condition<state>::result , body<state> , empty >::exec() ;
       If_exec< condition<state>::result,
         For< condition, increment, body, typename increment<state>::result >,
                empty
         >::exec() ;
    }
};