#include <boost/preprocessor/arithmetic/dec.hpp> #include <boost/preprocessor/arithmetic/mul.hpp> #include <boost/preprocessor/control/while.hpp> #include <boost/preprocessor/tuple/elem.hpp> #define PRED(d, data) BOOST_PP_TUPLE_ELEM(3, 0, data) #define OP(d, data) \ ( \ BOOST_PP_DEC( \ BOOST_PP_TUPLE_ELEM(3, 0, data) \ ), \ BOOST_PP_TUPLE_ELEM(3, 1, data), \ BOOST_PP_MUL_D( \ d, \ BOOST_PP_TUPLE_ELEM(3, 2, data), \ BOOST_PP_TUPLE_ELEM(3, 1, data) \ ) \ ) \ /**/ // raise 'x' to the 'n'-th power #define EXP(x, n) BOOST_PP_TUPLE_ELEM(3, 2, BOOST_PP_WHILE(PRED, OP, (n, x, 1))) EXP(4, 2) // expands to 16 EXP(2, 3) // expands to 8
Distributed under the Boost Software License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at www.boost.org/LICENSE_1_0.txt)