Boost logo

Boost :

From: Kamil Burzynski (K.Burzynski_at_[hidden])
Date: 2002-08-05 09:02:41


Hello.

    I'm a C++ developer, who is learning STL recently, and loving it :)
I've encountered Boost library, which seems to extending STL funcationality
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> {
public:
  explicit map_fun_t(_Ret (_Tp::*__pf)()) : _M_f(__pf) {}
 template< class _Mp> _Ret operator()(_Mp &__p) const { return ((__p.second)->*_M_f)(); }
private:
  _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."

Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk