Boost logo

Boost :

From: David Bergman (davidb_at_[hidden])
Date: 2002-08-05 10:09:40


If you are not satisified with std::mem_fun you can always use
boost::mem_fn, which is part of Boost.Bind. That accomplish what you
want, since it handles pointers (even smart ones...) to objects, just as

Otherwise, most Boost constructs dealing with function objects do handle
member functions quite well. Look at Boost.Bind, Boost.Function and
Boost.Lambda for both inspiration and tools for your future (hopefully
generic) work.

Happy programming,


-----Original Message-----
From: boost-bounces_at_[hidden]
[mailto:boost-bounces_at_[hidden]] On Behalf Of Kamil Burzynski
Sent: Monday, August 05, 2002 10:03 AM
To: boost_at_[hidden]
Subject: [boost] Newbie and his template for comments.


    I'm a C++ developer, who is learning STL recently, and loving it :)
I've encountered Boost library, which seems to extending STL
a lot :) I've also created a sampel template for usage in my company,
and I would like to get comments about it: is this code ok, exception
safe, somebody did it better things like that.

 In my projects I'm often using structures like map<T1, T2 *>, e.g.
map<int, Point *> or map<string, Person*> or something like this.
So, often I want to do something like that:

map<int, Point *> my_map;
map<int, Point *>::iterator it;

/* .. */

for( it=my_map.begin();it!=my_map.end();++it )
    it->second->Reset(); //sets x,y to 0,0

example may be stupid, but shows what I'm trying to explain.
To improve it I've created a template (given below), so I ca use it
like that:

for_each( my_map.begin(), my_map.end(), map_fun( &Point::Reset ) );

Which i think is much better. My template is as follows (style is
kept from GCC includes, so it may be not as readable as it could be :)

template <class _Ret, class _Tp>
class map_fun_t : public unary_function<_Tp*,_Ret> {
  explicit map_fun_t(_Ret (_Tp::*__pf)()) : _M_f(__pf) {}
 template< class _Mp> _Ret operator()(_Mp &__p) const { return
((__p.second)->*_M_f)(); }
  _Ret (_Tp::*_M_f)();

template <class _Ret, class _Tp>
inline map_fun_t<_Ret,_Tp> map_fun(_Ret (_Tp::*__f)())
  { return map_fun_t<_Ret,_Tp>(__f); }

What do you think about it? Has it any flaws? Somebody done it already?
Is it usable for anyone except me? :)

Thanks in advance.

Best regards from
Kamil Burzynski
Senior Design Engineer
Advanced Digital Broadcast Poland, LTD.
  - -
"Yes, I'm criminal. My crime is that of curiosity."
Unsubscribe & other changes:

Boost list run by bdawes at, gregod at, cpdaniel at, john at