Try this:
#include <boost/preprocessor/arithmetic/add.hpp>
#include <boost/preprocessor/control/while.hpp>
#include <boost/preprocessor/tuple/elem.hpp>
#include <boost/preprocessor/tuple/push_back.hpp>
#include <boost/preprocessor/tuple/reverse.hpp>
#define ARRAY_ELEM(x,n) x[n]
#define PRED(n, state) BOOST_PP_TUPLE_ELEM(3, 1, state)
#define OP(d, state)
OP_D(
d,
BOOST_PP_TUPLE_ELEM(3, 0, state),
BOOST_PP_TUPLE_ELEM(3, 1, state),
BOOST_PP_TUPLE_ELEM(3, 2, state)
)
#define OP_D(d, res, c, arr_name)
(
BOOST_PP_TUPLE_PUSH_BACK(res,ARRAY_ELEM(arr_name,BOOST_PP_DEC(c))),
BOOST_PP_DEC(c),
arr_name
)
#define UNPACK_REVERSE(ARR_NAME,N)
BOOST_PP_TUPLE_ELEM(
3, 0,
BOOST_PP_WHILE(PRED, OP, ((ARRAY_ELEM(ARR_NAME,BOOST_PP_DEC(N))),
BOOST_PP_DEC(N),ARR_NAME) )
)
/*The macro to be called*/
#define UNPACK(ARR_NAME,N)
BOOST_PP_TUPLE_REVERSE(UNPACK_REVERSE(ARR_NAME,N))
And here's a usage example:
void print3(int x, int y, int z){
printf("%d,%d,%d
",x,y,z);
}
int main(){
int z[3]={1,2,3};
print3 UNPACK(z,3);
return 0;
}
This macro uses Boost's Preprocessor library in a loop to generate and expand the array elements in a tuple. Hope it helps.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…