[英]transform_iterator compile problem
HI,你好,
I don't like posting compile problems, but I really can't figure this one out.我不喜欢发布编译问题,但我真的无法解决这个问题。 Using this code:
使用此代码:
#include <map>
#include <boost/iterator/transform_iterator.hpp>
using namespace std;
template <typename K, typename V>
struct get_value
{
const V& operator ()(std::pair<K, V> const& p) { return p.second; }
};
class test
{
typedef map<int, float> TMap;
TMap mymap;
public:
typedef get_value<TMap::key_type, TMap::value_type> F;
typedef boost::transform_iterator<F, TMap::iterator> transform_iterator;
transform_iterator begin()
{
return make_transform_iterator(mymap.begin(), F());
}
};
Getting this compile error:得到这个编译错误:
transform_iterator.hpp(43) : error C2039: 'result_type' : is not a member of 'get_value<K,V>'
with
[
K=int,
V=std::pair<const int,float>
]
Can anyone explain why this isn't working?谁能解释为什么这不起作用? I'm using Visual Studio 7.0 with boost 1.36.0
我使用的是带有 boost 1.36.0 的 Visual Studio 7.0
Thanks.谢谢。
Since you also asked for an explanation 既然你也要求解释
The transform_iterator
needs to know the return type of the function called in order to instantiate itself. transform_iterator
需要知道被调用函数的返回类型才能实例化自身。 This is determined via result_of
(found in <boost/utility/result_of.hpp>
这是通过
result_of
确定的(在<boost/utility/result_of.hpp>
If you use a function object, you need to define a member result_type
to specify the result type of the object. 如果使用函数对象,则需要定义成员
result_type
以指定对象的结果类型。 (since an object doesn't have a 'return type' as such) (因为对象没有'返回类型'这样)
If you would have used a regular function, result_of
would be able to figure it out on his own, eg: 如果您使用常规函数,
result_of
将能够自己解决,例如:
template <typename K, typename V>
const V & get_value(std::pair<K, V> const & p) { return p.second; }
class test
{
typedef map<int, float> TMap;
TMap mymap;
public:
typedef boost::function< const TMap::mapped_type & (const TMap::value_type &) > F;
typedef boost::transform_iterator<F, TMap::iterator> transform_iterator;
transform_iterator begin()
{
return boost::make_transform_iterator(mymap.begin(), &get_value< int, float >);
}
};
你必须从unary_function<const V&, std::pair<K, V> const&>
继承get_value
,告诉transform_iterator
get_value
的签名是什么。
// here is a working example:
#include <vector>
#include <iostream>
#include <boost/iterator/transform_iterator.hpp>
template <typename T, typename U>
const T& Get1st(const std::pair<T, U>& pair) { return pair.first; }
struct Bar {
using Pairs = std::vector<std::pair<int, char>>;
using Iter = boost::transform_iterator< decltype(&Get1st<int, char>), Pairs::const_iterator >;
void add(int i, char c) { _pairs.emplace_back(i, c); }
Iter begin() { return boost::make_transform_iterator(_pairs.begin(), &Get1st<int, char>); }
Iter end() { return boost::make_transform_iterator(_pairs.end(), &Get1st<int, char>); }
private:
Pairs _pairs;
};
int main() {
Bar bar;
bar.add(1, 'a');
bar.add(3, 'c');
bar.add(2, 'b');
for(const auto& i : bar) std::cout << i << " ";
std::cout << "\n";
return 0;
}
// outputs: 1, 3, 2 // 输出:1, 3, 2
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.