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

标签 c++ boost boost-preprocessor

我定义了一个宏

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

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

int height = 10;
int width = 20;

最多我能得到的是 height,int,10width,int,20 作为字符串,但不能解析单个元素。

最佳答案

在处理之前使用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

关于c++ - 使用 Boost 预处理器解析元素序列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39766077/

相关文章:

c++ - boost::spirit qi::uint_有效数字范围

c++ - 如何使用预处理器从源文件中获取文本行?

C++如何为 float 设置固定的小数精度

c++ - 0x80020009 OLE 异常访问除 0 以外的任何数字

elasticsearch - 通过整数字段 boost 结果

c++ - Boost.预处理器 : BOOST_PP_TUPLE_ELEM and BOOST_PP_SEQ_ELEM

c++ - Boost 预处理器程序在从 Boost 1.55 更改为 1.57 后无法编译

c++ - 使用 Boost.Spirit.Lex 和流迭代器

c++ - 从输入容器/迭代器推导 lambda 参数类型

c++ - 使用 boost::optional 的正确方法