我有一个定义的宏
#define TYPES (height,int,10)(width,int,20)
如何使用像这样的Boost预处理器来扩展这个宏?
int height = 10;
int width = 20;
我能得到的是 height,int,10 和 width,int,20 作为字符串但不能解析单个元素。
答案 0 :(得分:4)
在处理之前,使用一小撮自定义宏将TYPES
变为((height,int,10))((width,int,20))
,以便BOOST_PP_SEQ_FOR_EACH
不会阻塞它:
#define GLK_PP_DETAIL_SEQ_DOUBLE_PARENS_0(...) \
((__VA_ARGS__)) GLK_PP_DETAIL_SEQ_DOUBLE_PARENS_1
#define GLK_PP_DETAIL_SEQ_DOUBLE_PARENS_1(...) \
((__VA_ARGS__)) GLK_PP_DETAIL_SEQ_DOUBLE_PARENS_0
#define GLK_PP_DETAIL_SEQ_DOUBLE_PARENS_0_END
#define GLK_PP_DETAIL_SEQ_DOUBLE_PARENS_1_END
// Double the parentheses of a Boost.PP sequence
// I.e. (a, b)(c, d) becomes ((a, b))((c, d))
#define GLK_PP_SEQ_DOUBLE_PARENS(seq) \
BOOST_PP_CAT(GLK_PP_DETAIL_SEQ_DOUBLE_PARENS_0 seq, _END)
#define MAKE_ONE_VARIABLE(r, data, elem) \
BOOST_PP_TUPLE_ELEM(1, elem) BOOST_PP_TUPLE_ELEM(0, elem) = BOOST_PP_TUPLE_ELEM(2, elem);
#define MAKE_VARIABLES(seq) \
BOOST_PP_SEQ_FOR_EACH(MAKE_ONE_VARIABLE, ~, GLK_PP_SEQ_DOUBLE_PARENS(seq))
用法:
#define TYPES (height,int,10)(width,int,20)
int main() {
MAKE_VARIABLES(TYPES)
}
预处理:
int main() {
int height = 10; int width = 20;
}