C++ 在助推融合序列上使用范围
我正在尝试打印C++ 在助推融合序列上使用范围,c++,c++11,boost-fusion,C++,C++11,Boost Fusion,我正在尝试打印struct成员,如下所示: #include <iostream> #include <boost/fusion/adapted/struct/adapt_struct.hpp> #include <boost/fusion/include/adapt_struct.hpp> struct Node { int a = 4; double b = 2.2; }; BOOST_FUSION_ADAPT_STRUCT(Node,
struct
成员,如下所示:
#include <iostream>
#include <boost/fusion/adapted/struct/adapt_struct.hpp>
#include <boost/fusion/include/adapt_struct.hpp>
struct Node {
int a = 4;
double b = 2.2;
};
BOOST_FUSION_ADAPT_STRUCT(Node, a, b)
int main() {
Node n;
for (auto el: n) { // What do I put instead of n here?
std::cout << el << std::endl;
}
return 0;
}
#包括
#包括
#包括
结构节点{
INTA=4;
双b=2.2;
};
BOOST\u FUSION\u ADAPT\u结构(节点a、b)
int main(){
节点n;
对于(自动el:n){//我应该在这里放置什么来代替n?
std::cout在这种情况下,您不能使用基于范围的for
。它是元编程,每个成员迭代器都有自己的类型。您可以使用fusion::for_each
或使用手写结构进行遍历
#include <iostream>
#include <boost/fusion/adapted/struct/adapt_struct.hpp>
#include <boost/fusion/include/adapt_struct.hpp>
#include <boost/fusion/include/for_each.hpp>
struct Node {
int a = 4;
int b = 2.2;
};
BOOST_FUSION_ADAPT_STRUCT(Node, a, b)
struct printer
{
template<typename T>
void operator () (const T& arg) const
{
std::cout << arg << std::endl;
}
};
int main() {
Node n;
boost::fusion::for_each(n, printer());
return 0;
}
#包括
#包括
#包括
#包括
结构节点{
INTA=4;
int b=2.2;
};
BOOST\u FUSION\u ADAPT\u结构(节点a、b)
结构打印机
{
模板
void运算符()(常量T和arg)常量
{
std::cout Nice!有没有办法找到printer::operator()
)中的字段名称?@AlwaysLearning no.对于这种情况,您应该编写自己的元结构,这样可以从fusion::begin
到fusion::end
遍历结构并调用特殊的元函数。