【发布时间】:2015-07-29 19:00:32
【问题描述】:
我想在编译时迭代 struct 并写入输出迭代次数。顺便提一下 - 在实际情况下,我会在数据中传递更多参数。
#include <iostream>
#include <string>
#include <vector>
#include <boost/fusion/include/adapt_struct.hpp>
#include <boost/fusion/include/size.hpp>
#include <boost/preprocessor/repetition/repeat.hpp>
struct MyStruct
{
int x;
int y;
};
BOOST_FUSION_ADAPT_STRUCT(
MyStruct,
(int, x)
(int, y)
)
#define PRINT(unused, number, data) \
std::cout << number << std::endl;
int main()
{
MyStruct s;
std::cout << boost::fusion::size(s) << std::endl;
//line below works - it iterate and write output
BOOST_PP_REPEAT(2, PRINT, "here I will pass my data")
//this won't compile
//BOOST_PP_REPEAT(boost::fusion::size(s), PRINT, "here i will pass my data")
}
如何修复有问题的行,以便当我在结构中添加更多成员时它会起作用?我需要 C++03 的解决方案 :(
【问题讨论】:
-
大小函数应该在预处理阶段计算,目前不是这种情况。你为什么不改用 fusion::for_each 呢?
-
在实际情况下,我想构建简单的开关 - 每个案例都是由 BOOST_PP_REPEAT 创建的 - fusion::for_each 看起来。但是,如果您可以使用 boost::fusion::for_each 显示 switch 语句-那么我可以使用它:) (仅显示如何加载多个参数,以便我可以在 case 语句中使用它(例如 std::cout
标签: c++ boost-fusion boost-preprocessor