Problem with transform_iterator compilation

Hi

I don't like compilation messages, but I really can't figure it 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 compilation 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 is not working? I am using Visual Studio 7.0 with boost 1.36.0

Thanks.

+4
source share
2 answers

Since you also asked for an explanation

transform_iterator must know the return type of the function called in order to instantiate. This is determined using result_of (found in <boost/utility/result_of.hpp>

If you use a function object, you need to define the result_type member to indicate the type of the result of the object. (since the object does not have a "return type" as such)

If you used a regular function, result_of could figure it out yourself, for example:

 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 >); } }; 
+7
source

You need to inherit get_value from unary_function<const V&, std::pair<K, V> const&> to tell transform_iterator what the get_value signature get_value .

+6
source

All Articles