transform_iterator编译问题 [英] 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());
}
};
获得此编译错误:
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>
]
任何人都可以解释为什么这不工作?我使用Visual Studio 7.0 with boost 1.36.0
Can anyone explain why this isn't working? I'm using Visual Studio 7.0 with boost 1.36.0
感谢。
推荐答案
因为你也要求一个解释
transform_iterator
需要知道函数的返回类型调用以便实例化自身。这是通过 result_of
(位于< boost / utility / result_of.hpp>
The transform_iterator
needs to know the return type of the function called in order to instantiate itself. This is determined via result_of
(found in <boost/utility/result_of.hpp>
如果使用函数对象,则需要定义一个成员 result_type
来指定对象的结果类型(因为对象没有返回类型
If you use a function object, you need to define a member result_type
to specify the result type of the object. (since an object doesn't have a 'return type' as such)
如果你使用一个常规函数, result_of
就可以自己计算出来,例如:
If you would have used a regular function, result_of
would be able to figure it out on his own, e.g.:
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 >);
}
};
这篇关于transform_iterator编译问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!