首页 > 代码库 > boost学习之BOOST_PP_SEQ_FOR_EACH

boost学习之BOOST_PP_SEQ_FOR_EACH

用途:

BOOST_PP_SEQ_FOR_EACH宏,用于将一个序列中参数依次按照指定宏进行展开。

语法:

BOOST_PP_SEQ_FOR_EACH(macro, data, seq)

变量:

macro

一个以格式macro(rdataelem)定义的三元宏。该宏被BOOST_PP_SEQ_FOR_EACH按照seq中每个元素进行展开。展开该宏,需要用到下一个BOOST_PP_FOR的重复项、备用数据data和当前元素。

data

备用数据,用于传给macro

seq

用于供macro按照哪个序列进行展开

用法:

BOOST_PP_SEQ_FOR_EACH是一个重复项的宏。

如果序列是(a)(b)(c),则展开为:

macro(rdataamacro(rdatabmacro(rdatac)

如果要求更高效率,可使用BOOST_PP_SEQ_FOR_EACH_R

头文件:

<boost/preprocessor/seq/for_each.hpp>

示例代码:

1.例1

#include <boost/preprocessor/cat.hpp>
#include <boost/preprocessor/seq/for_each.hpp>

#define SEQ (w)(x)(y)(z)

#define MACRO(r, data, elem) BOOST_PP_CAT(elem, data)

BOOST_PP_SEQ_FOR_EACH(MACRO, _, SEQ) // expands to w_ x_ y_ z_

2.例2

#include <boost/preprocessor/cat.hpp>
#include <boost/preprocessor/seq/for_each.hpp>

#define SEQ (w)(x)(y)(z)

#define MACRO(r, data, elem) elem::GetInstance()

BOOST_PP_SEQ_FOR_EACH(MACRO, _, SEQ) // expands to w::GetInstance() x::GetInstance() y::GetInstance() z::GetInstance()


boost学习之BOOST_PP_SEQ_FOR_EACH