上を踏まえて実際にやりかったのが以下.

#include <boost/utility/enable_if.hpp>
#include <boost/mpl/arithmetic.hpp>
#include <boost/mpl/bitwise.hpp>
#include <boost/mpl/comparison.hpp>
#include <boost/mpl/int.hpp>
#include <iostream>

typedef boost::mpl::int_<0> zero;
typedef boost::mpl::int_<1> one;

// pow(m, n) = pow(m, n & 1) * pow(m, n >> 1) * pow(m, n >> 1)
template<class T1, class T2, class = void>
struct pow
  : public
  boost::mpl::multiplies<
    typename pow<
      T1,
      typename boost::mpl::bitand_<T2, one>::type
    >::type,
    typename pow<
      T1,
      typename boost::mpl::shift_right<T2, one>::type
    >::type,
    typename pow<
      T1,
      typename boost::mpl::shift_right<T2, one>::type
    >::type
  >
{ };

// pow(m, 0) = 1
template<class T1, class T2>
struct pow<
  T1,
  T2,
  typename boost::enable_if<typename boost::mpl::equal_to<T2, zero>::type>::type
>
{
  typedef one type;
};

// pow(m, 1) = n
template<class T1, class T2>
struct pow<
  T1,
  T2,
  typename boost::enable_if<typename boost::mpl::equal_to<T2, one>::type>::type
>
{
  typedef T1 type;
};

int main()
{
  std::cout
    << pow<boost::mpl::int_<2>, boost::mpl::int_<10> >::type::value
    << std::endl; // pow(2, 10) = 1024
  std::cout
    << pow<boost::mpl::int_<3>, boost::mpl::int_<6> >::type::value
    << std::endl; // pow(3, 6) = 729
}

(゜д゜)ウマー.

  • pow(m, n) = pow(m, n & 1) * pow(m, n >> 1) * pow(m, n >> 1)
  • pow(m, 0) = 1
  • pow(m, 1) = m

で定義される数列をメタ関数で表現したもの.要するにpow(m, n) = m^n.もちろん完全にコンパイル時計算.