transform_iterator compile problem

前端 未结 2 957
误落风尘
误落风尘 2021-01-18 16:35

HI,

I don\'t like posting compile problems, but I really can\'t figure this one out. Using this code:

#include 
#include 

        
2条回答
  •  忘掉有多难
    2021-01-18 17:00

    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

    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 
    const V & get_value(std::pair const & p)  { return p.second; }
    
    class test
    {
      typedef map TMap;
      TMap mymap;
    
    public:
      typedef boost::function< const TMap::mapped_type & (const  TMap::value_type &)  > F;
      typedef boost::transform_iterator transform_iterator;
    
      transform_iterator begin()
      {
        return boost::make_transform_iterator(mymap.begin(), &get_value< int, float >);
      }
    };
    

提交回复
热议问题