如何使用boost::adaptors:: transforms从模板化的类和向量生成范围?
How do I use boost::adaptors::transformed to produce a range from a templated class and a vector?
我问了一个关于使用lambda来实现类似的东西的问题,但是不能得到这个工作,所以我尝试使用函子来解决问题。无论如何,这可能更简洁,因为它不涉及构造std::function
对象,并且更接近文档中列出的示例。
下面是一个简单的设置,说明了我的问题:
#include <boost/range/adaptor/transformed.hpp>
#include <vector>
// some structure
template <typename T>
struct MyStruct
{
MyStruct(T t)
{
}
};
template <typename T>
struct Converter
{
using return_type = MyStruct<T>;
return_type operator()(const T& value)
{
return return_type(value);
}
};
int main(int argc, const char* argv[])
{
std::vector<int> vec {1, 2, 3};
auto val = vec | boost::adaptors::transformed(Converter<int>());
return 0;
}
当我尝试编译这个时,我得到以下错误消息:
/home/user/packages/boost/mpl/eval_if.hpp:38:31: error: no type named .hpp' type ' in '
boost::mpl::eval_if<boost::is_same<boost::use_default, boost::use_default>, boost::result_of<const Converter<int>(int&)>, boost::mpl::identity<boost::use_default> >::f_ {aka struct boost::result_of<const Converter<int>(int&)>}
'
我不知道这是怎么回事。我无法发现代码中的任何错误。什么好主意吗?
错误告诉您boost::result_of<const Converter<int>(int&)>
没有type
成员。换句话说,当使用const Converter<int>
对象时,函数调用操作符不起作用。一旦您知道了问题所在,就很容易发现问题所在:
return_type operator()(const T& value) const
// ^^^^^
{
return return_type(value);
}
相关文章:
- 尝试通过多个向量访问变量时,向量下标超出范围
- 基于范围的 for 循环:迭代使用一个元素扩展的向量
- C++ - 使用基于范围的 for 循环将字符值分配给向量中的字符串不会分配值
- 布尔值向量的基于范围 for 循环
- 如何使用count_if计算向量中的可变数字范围
- 范围 v3 的和向量
- 关于在向量向量上使用基于范围的 for 循环
- 使用基于范围的指针循环和向量时出现C++错误
- 用基于范围的for循环填充指针向量
- 创建结构体向量,表达式:向量下标超出范围
- 代码超出范围.如何动态分配此向量?
- 无法取消引用超出范围的向量迭代器 - 有什么问题?
- 非常量对象的向量似乎在基于范围的 for 循环中被视为常量
- 从给定范围内的向量中查找最大元素
- 向量向量的范围范围
- 将局部变量保存到全局向量中,为什么离开局部范围后可以得到这些局部变量?
- 指针范围问题和返回类中封装的指针向量内的指针引用
- 在 c++ 中检查 if 条件中的向量位置范围
- 向量下标超出向量向量的范围
- 如何正确使用向量范围构造函数