在助推聚变序列上使用的范围
Using range for on a boost FUSION sequence
本文关键字:范围 更新时间:2023-10-16
我正在尝试按如下方式打印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;
}
这当然是错误的,因为n
只是struct
。如何为range for
而不是n
工作的序列设置?
在这种情况下不能使用range-based for
。这是元编程,每个成员迭代器都有自己的类型。您可以使用fusion::for_each
进行遍历,也可以使用hand-writen结构进行遍历。
#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;
}
相关文章:
- 为什么在全局范围内使用"extern int a"似乎不行?
- 尝试通过多个向量访问变量时,向量下标超出范围
- 错误:未在此范围内声明'reverse'
- 正在将指针转换为范围
- 使用std::transform将一个范围的元素添加到另一个范围中
- 在基于范围的for循环中使用结构化绑定声明
- 如何计算数据类型的范围,例如int
- 为什么 const std::p air<K,V>& 在 std::map 上基于范围的 for 循环不起作用?
- 在C++中查找范围的长度
- 如何设置一个范围来提取我想要获得的信息
- 并行用于C++17中数组索引范围内的循环
- 为左值和右值的包装器实现C++范围
- 求出有多少个数字是完美平方,而sqrt()是L,R范围内的素数
- 关于:C++中异常对象的范围:为什么我没有得到副本?
- 超出范围时使用对象
- 不计算一个范围内的完美数
- ";结果类型必须是可从输入范围的值类型""构造的;创建std::vector时
- 基于范围的 for 循环:迭代使用一个元素扩展的向量
- 如何访问超出其块范围的对象?
- 矢量下标超出SFML游戏中的范围