使用Boost Preprocessor解析元素序列 [英] Use Boost Preprocessor to Parse sequence of elements

查看:252
本文介绍了使用Boost Preprocessor解析元素序列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我定义了一个宏

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

如何使用Boost Preprocessor扩展此宏?

How to expand this macro using Boost Preprocessor something like this?

int height = 10;
int width = 20;

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

at most i am able to get is height,int,10 and width,int,20 as string but can't parse individual element.

推荐答案

在处理前使用BOOST_PP_VARIADIC_SEQ_TO_SEQTYPES转换为((height,int,10))((width,int,20)),以使BOOST_PP_SEQ_FOR_EACH不会窒息:

Using BOOST_PP_VARIADIC_SEQ_TO_SEQ to turn TYPES into ((height,int,10))((width,int,20)) before processing, so that BOOST_PP_SEQ_FOR_EACH doesn't choke on it:

#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;
}

在Coliru上实时观看

这篇关于使用Boost Preprocessor解析元素序列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆