使用 Boost 预处理器解析元素序列

Use Boost Preprocessor to Parse sequence of elements

我定义了一个宏

#define TYPES (height,int,10)(width,int,20)

如何使用 Boost 预处理器扩展这个宏?

int height = 10;
int width = 20;

最多我能得到的是 height,int,10width,int,20 as string 但不能解析单个元素。

在处理前使用BOOST_PP_VARIADIC_SEQ_TO_SEQTYPES变成((height,int,10))((width,int,20)),这样BOOST_PP_SEQ_FOR_EACH就不会卡住了:

#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, ~, BOOST_PP_VARIADIC_SEQ_TO_SEQ(seq))

用法:

#define TYPES (height,int,10)(width,int,20)

int main() {
    MAKE_VARIABLES(TYPES)
}

被预处理成:

int main() {
    int height = 10; int width = 20;
}

See it live on Coliru