6

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

Thanks.

Piotr Dobrogost
  • 41,292
  • 40
  • 236
  • 366
Dan
  • 33,953
  • 24
  • 61
  • 87

3 Answers3

7

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. This is determined via result_of (found in <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. (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, 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 >);
  }
};
Pieter
  • 17,435
  • 8
  • 50
  • 89
  • boost::function gives me quite a performance hit, so I've ended up subclassing the boost::iterator_adaptor instead to do this, which has turned out to be a little more efficient, but thanks. – Dan Feb 18 '09 at 23:45
  • @Dan Is it possible for you to edit your question to include the answer you went with? Pretty sure you've forgotten it by now, but one can hope :) – TCSGrad Nov 13 '18 at 17:18
6

You'll have to inherit get_value from unary_function<const V&, std::pair<K, V> const&> to tell transform_iterator what the signature of get_value is.

Eclipse
  • 44,851
  • 20
  • 112
  • 171
0
// 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

Zhi
  • 1
  • 1
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Oct 14 '21 at 00:38