Boost logo

Boost Users :

From: David Abrahams (dave_at_[hidden])
Date: 2004-08-15 21:17:08


goochrules! <goochrules_at_[hidden]> writes:

>> > You can also use mem_fn:
>> >
>> > for_each( forest.begin(), forest.end(), mem_fn(&tree::grow) );
>> >
>> > or bind, if grow takes arguments:
>> >
>> > for_each( forest.begin(), forest.end(), bind(&tree::grow, _1, 5) );
>>
>> And, BTW, your iterator is not conforming and doesn't seem to be doing
>> what you want. See
>> http://www.boost.org/libs/iterator/doc/indirect_iterator.html for a
>> conforming solution.
>
> Indeed, boost::shared_ptr and boost::idirect_iterator are exactly what
> I'm looking for.
>
> But I'm at a complete loss as to how to get them to work with std::for_each:
>
> 36: typedef boost::shared_ptr< tree > item_t;
> 37: typedef std::list< item_t > list_t;
> 38: typedef std::list< item_t >::iterator iter_t;
> 39: list_t forest;
> 40:
> 41: forest.push_back(item_t(new oak));
> 42: forest.push_back(item_t(new pine));
> 43:
> 44: boost::indirect_iterator< iter_t, tree > first(forest.begin()),
> 45: last(forest.end());
> 46: std::for_each(first, last, std::mem_fun(&tree::grow));
>
>> g++ -Wall -O0 -o inherit inherit.cpp
> /usr/include/gcc/darwin/3.3/c++/bits/stl_algo.h: In function `_Function
> std::for_each(_InputIter, _InputIter, _Function) [with _InputIter =
> boost::indirect_iterator<main(int, char**)::iter_t, tree,
> boost::use_default, boost::use_default, boost::use_default>, _Function =
> std::mem_fun_t<void, tree>]':
> inherit.cpp:46: instantiated from here
> /usr/include/gcc/darwin/3.3/c++/bits/stl_algo.h:166: error: no match for call
> to `(std::mem_fun_t<void, tree>) (tree&)'
> /usr/include/gcc/darwin/3.3/c++/bits/stl_function.h:613: error: candidates are:
> void std::mem_fun_t<void, _Tp>::operator()(_Tp*) const [with _Tp = tree]

Use boost::mem_fn instead. I think you don't even need
indirect_iterator in that case... but if you want to keep using
indirect_iterator, I think std::mem_fun_ref is what you're after.

-- 
Dave Abrahams
Boost Consulting
http://www.boost-consulting.com

Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net