代码:
#include <boost/preprocessor/seq/for_each.hpp>
#include <boost/preprocessor/stringize.hpp>
// 定义打印宏
//计数器 r、数据参数 data 和当前元素 elem
#define PRINT_ARG(r, data, elem) \
std::cout << "Arg " << r << ": " << BOOST_PP_STRINGIZE(data)<<" "<<BOOST_PP_STRINGIZE(elem) << std::endl;
// 定义结构体
struct MyStruct2 {
int x;
double y;
const char* name;
};
// 定义可变参数列表
#define MY_ARGS2 (1)(2.5)("hello")(MyStruct2{
3, 4.5, "world"})
// 打印可变参数列表
void print_args2() {
int i = 0;
double d = 0;
const char* str = nullptr;
MyStruct2 s;
BOOST_PP_SEQ_FOR_EACH(PRINT_ARG, i, MY_ARGS2);
BOOST_PP_SEQ_FOR_EACH(PRINT_ARG, d, MY_ARGS2);
BOOST_PP_SEQ_FOR_EACH(PRINT_ARG, str, MY_ARGS2);
BOOST_PP_SEQ_FOR_EACH(PRINT_ARG, s, MY_ARGS2);//重复项、备用数据data和当前元素。
}
int
main(int argc, char** argv)
{
print_args2();
return (0);
}
(处理宏,增加信息data,列表)根据列表的元素依次展开,列表一般是底下的形式:
// 定义可变参数列表
#define MY_ARGS2 (1)(2.5)("hello")(MyStruct2{
3, 4.5, "world"})
输出结果如下,暂时不知道为什么从2开始,不过这个序号只是计数的: