BOOST_PP_SEQ_FOR_EACH
is not reentrant. There are only a few macros in Boost.PP that are reentrant(BOOST_PP_FOR
, BOOST_PP_WHILE
, and BOOST_PP_REPEAT
). However, you can workaround it by using deferred expressions, like this:
#include <boost/preprocessor.hpp>
#define EXPAND(...) __VA_ARGS__
#define EMPTY()
#define DEFER(x) x EMPTY()
// An indirection macro to avoid direct recursion
#define BOOST_PP_SEQ_FOR_EACH_ID() BOOST_PP_SEQ_FOR_EACH
#define ARGS(r, data, elem) \
BOOST_PP_COMMA_IF(BOOST_PP_SUB(r, 2)) \
BOOST_PP_SEQ_ELEM(0, elem) BOOST_PP_SEQ_ELEM(1, elem)
// Defer BOOST_PP_SEQ_FOR_EACH_ID here
#define DEF_FUN(name, args) void name(DEFER(BOOST_PP_SEQ_FOR_EACH_ID)()(ARGS,,args));
#define DEF_FUNCTIONS_ELEM(r, data, elem) DEF_FUN(BOOST_PP_SEQ_ELEM(0, elem), BOOST_PP_SEQ_ELEM(1, elem))
// Add EXPAND here to apply another scan to expand the deferred expression
#define DEF_FUNCTIONS(funSeqs) \
EXPAND(BOOST_PP_SEQ_FOR_EACH(DEF_FUNCTIONS_ELEM,, funSeqs))
DEF_FUNCTIONS
(
((fun0) (((int)(arg0)) ((char)(arg1))))
((fun1) (((char)(arg0)) ((long)(arg1)) ((short)(arg2))))
((fun3) ())
)