本文介绍了如何使用boost :: preprocessor解压缩序列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何使用boost :: preprocessor解压缩序列对?
How to use boost::preprocessor to unzip a sequence of pairs?
例如,我有一个如下序列(两者之间的逗号无关紧要)
For example, I have a sequence as below (comma between doesn't matter)
(int,x)(double,y)(float,z) or
(int,x),(double,y),(float,z) or
((int)(x))((double)(y))((float)(z))
并想要转换为
int,double,float
和
x,y,z
通过使用macor之类的
By using macor like
UNZIP(i, seq)
其中i
是索引.
推荐答案
(int, x, 10)(double, y, 20)(float, z, 30)
的解压缩,即元素之间没有逗号的序列.
Unzipping of (int, x, 10)(double, y, 20)(float, z, 30)
, i.e. sequence without commas between elements.
#include <boost/preprocessor/punctuation/comma_if.hpp>
#include <boost/preprocessor/seq/for_each_i.hpp>
#include <boost/preprocessor/seq/pop_front.hpp>
#include <boost/preprocessor/seq/for_each.hpp>
#include <boost/preprocessor/variadic/elem.hpp>
#include <boost/preprocessor/cat.hpp>
// Such technique is used at:
// http://www.boost.org/doc/libs/1_56_0/boost/fusion/adapted/struct/define_struct.hpp
#define AUXILIARY_0(...) ((__VA_ARGS__)) AUXILIARY_1
#define AUXILIARY_1(...) ((__VA_ARGS__)) AUXILIARY_0
#define AUXILIARY_0_END
#define AUXILIARY_1_END
#define REMOVE_PARENTHESES(...) __VA_ARGS__
#define COMMA_SEPARATED(r, data, i, elem) \
BOOST_PP_COMMA_IF(i) BOOST_PP_VARIADIC_ELEM(data, REMOVE_PARENTHESES elem) \
/**/
#define ZIPPED_TO_SEQ(zipped) \
BOOST_PP_SEQ_POP_FRONT(BOOST_PP_CAT(AUXILIARY_0(0)zipped,_END)) \
/**/
#define FOR_EACH_ZIPPED_I(macro, data, zipped) \
BOOST_PP_SEQ_FOR_EACH_I(macro, data, ZIPPED_TO_SEQ(zipped)) \
/**/
#define UNZIP(i, zipped) FOR_EACH_ZIPPED_I(COMMA_SEPARATED, i, zipped)
/*******************************************************************/
// DEMO:
#define zipped (int, x, 10)(double, y, 20)(float, z, 30)
FIRST: UNZIP(0, zipped)
SECOND: UNZIP(1, zipped)
THIRD: UNZIP(2, zipped)
预处理器输出:
FIRST: int , double , float
SECOND: x , y , z
THIRD: 10 , 20 , 30
这篇关于如何使用boost :: preprocessor解压缩序列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!