Boost logo

Boost Users :

From: goochrules! (goochrules_at_[hidden])
Date: 2004-08-15 13:56:43


> > 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]

-- Matthew Peltzer
-- goochrules_at_[hidden]


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