This macro expands to the sequence:
macro(r, state) macro(r, op(r, state)) ... macro(r, op(r, ... op(r, state) ... ))
At certain times, it may be necessary to perform the concatenation with
BOOST_PP_CAT rather than the preprocessor token-pasting operator.
This happens when the
r value is a macro invocation itself.
It needs a delay to allow it to expand.
The syntax in such a scenario becomes:
BOOST_PP_CAT(BOOST_PP_FOR_, r)(state, pred, op, macro)
#include <boost/preprocessor/arithmetic/dec.hpp>
#include <boost/preprocessor/arithmetic/inc.hpp>
#include <boost/preprocessor/comparison/not_equal.hpp>
#include <boost/preprocessor/punctuation/comma_if.hpp>
#include <boost/preprocessor/repetition/for.hpp>
#include <boost/preprocessor/tuple/elem.hpp>
#define PRED(r, state) /* ............ */ \
BOOST_PP_NOT_EQUAL( \
BOOST_PP_TUPLE_ELEM(4, 0, state), \
BOOST_PP_INC( \
BOOST_PP_TUPLE_ELEM(4, 1, state) \
) \
) \
/**/
#define OP(r, state) /* .............. */ \
( \
BOOST_PP_INC( \
BOOST_PP_TUPLE_ELEM(4, 0, state) \
), \
BOOST_PP_TUPLE_ELEM(4, 1, state), \
BOOST_PP_TUPLE_ELEM(4, 2, state), \
BOOST_PP_INC( \
BOOST_PP_TUPLE_ELEM(4, 3, state) \
) \
) \
/**/
#define MACRO(r, state) /* ....................... */ \
BOOST_PP_COMMA_IF( \
BOOST_PP_TUPLE_ELEM(4, 3, state) \
) template< \
BOOST_PP_FOR_ ## r( \
(0, BOOST_PP_TUPLE_ELEM(4, 0, state), _, 0), \
PRED_2, OP, MACRO_2 \
) \
> class BOOST_PP_CAT( \
BOOST_PP_TUPLE_ELEM(4, 2, state), \
BOOST_PP_TUPLE_ELEM(4, 0, state) \
) \
/**/
#define PRED_2(r, state) /* ........ */ \
BOOST_PP_NOT_EQUAL( \
BOOST_PP_TUPLE_ELEM(4, 0, state), \
BOOST_PP_TUPLE_ELEM(4, 1, state) \
) \
/**/
#define MACRO_2(r, state) /* ...... */ \
BOOST_PP_COMMA_IF( \
BOOST_PP_TUPLE_ELEM(4, 0, state) \
) class \
/**/
#define TEMPLATE_TEMPLATE(low, high, name) /* ... */ \
BOOST_PP_FOR( \
(low, high, name, 0), \
PRED, OP, MACRO \
) \
/**/
TEMPLATE_TEMPLATE(2, 4, T)
/*
expands to...
template<class, class> class T2,
template<class, class, class> class T3,
template<class, class, class, class> class T4
*/