【问题标题】:How to zip multiple Boost Preprocessor sequences?如何压缩多个 Boost 预处理器序列?
【发布时间】:2017-02-01 18:39:33
【问题描述】:
给定多个 Boost 预处理器序列:
#define S1 (a)(b)(c)
#define S2 (1)(2)(3)
#define S3 (x1)(x2)(x3)
如何使用预处理器对它们进行压缩以使最终结果为((a)(1)(x1))((b)(2)(x2))((c)(3)(x3))?
注意事项
- 我想出了自己的答案,但我愿意在合理的时间内接受比我在下面的答案中提供的更好的解决方案。
- 请不要删除 c 标记,因为这个 Boost.Preprocessor 可以很好地与 C++ 和 C 一起工作,而且我的问题针对这两种语言。
【问题讨论】:
标签:
c
c++
c
c-preprocessor
boost-preprocessor
【解决方案1】:
您可以为此目的定义一个SEQ_ZIP 宏,如下所示:
#include <boost/preprocessor/control/expr_if.hpp>
#include <boost/preprocessor/repetition/repeat.hpp>
#include <boost/preprocessor/seq/elem.hpp>
#include <boost/preprocessor/seq/for_each.hpp>
#include <boost/preprocessor/seq/size.hpp>
#define SEQ_ZIP_NTH_(_,d,e) (BOOST_PP_SEQ_ELEM(d, e))
#define SEQ_ZIP_NTHS_(_,i,ss) (BOOST_PP_SEQ_FOR_EACH(SEQ_ZIP_NTH_,i,ss))
#define SEQ_ZIP_2(ne,ss) BOOST_PP_REPEAT(ne, SEQ_ZIP_NTHS_, ss)
#define SEQ_ZIP_(ne,ss) BOOST_PP_EXPR_IF(ne, SEQ_ZIP_2(ne, ss))
#define SEQ_ZIP(ss) SEQ_ZIP_(BOOST_PP_SEQ_SIZE(BOOST_PP_SEQ_ELEM(0,ss)), ss)
并使用它来压缩您的序列,如下所示:
#define S1 (a)(b)(c)
#define S2 (1)(2)(3)
#define S3 (x1)(x2)(x3)
SEQ_ZIP((S1)(S2)(S3))