如何使用boost :: adaptors :: transformed从模板类和向量生成范围? [英] How do I use boost::adaptors::transformed to produce a range from a templated class and a vector?
问题描述
我问了一个有关使用lambda早期实现类似功能的问题,但文档中列出的示例案例.
I asked a question about using a lambda to achieve something similar earlier but wasn't able to get this working so I've tried approach the problem using a functor instead. This is probably neater anyway in the sense that it doesn't involve constructing std::function
objects and is closer to the example case set out in the documentation.
这是一个简单的设置,它说明了我的问题:
Here is a simple setup that illustrates my problem:
#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;
}
当我尝试对此进行编译时,出现以下错误消息:
When I try to compile this I am getting the following error message:
/home/user/packages/boost/mpl/eval_if.hpp:38:31:错误:未命名类型 "
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&)>}
"
/home/user/packages/boost/mpl/eval_if.hpp:38:31: error: no type named ‘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&)>}
’
我不确定该怎么做.我无法在代码中发现任何错误.有什么想法吗?
I'm not sure what to make of this. I can't spot any errors in the code. Any ideas?
推荐答案
该错误告诉您boost::result_of<const Converter<int>(int&)>
没有type
成员.换句话说,使用const Converter<int>
对象时,函数调用运算符不起作用.一旦知道问题所在,就很容易发现问题所在:
The error tells you boost::result_of<const Converter<int>(int&)>
doesn't have the type
member. In other words, the function call operator doesn't work when a const Converter<int>
object is used. Once you know the problem, it's easy to see what's wrong:
return_type operator()(const T& value) const
// ^^^^^
{
return return_type(value);
}
这篇关于如何使用boost :: adaptors :: transformed从模板类和向量生成范围?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!