Newbie and his template for comments.

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."

--- In Boost-Users@y..., "Kamil Burzynski" <K.Burzynski@a...> wrote:
[...] 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.
It's so useful that it's already in Boost. :)
for_each( my_map.begin(), my_map.end(), map_fun( &Point::Reset ) );
for_each(my_map.begin(), my_map.end(), boost::bind(&Point::Reset, _1)); See also boost/function.hpp and lambda/lambda.hpp. Cool stuff! Ken

"kthomases" <ken@codeweavers.com> writes:
for_each(my_map.begin(), my_map.end(), boost::bind(&Point::Reset, _1));
Does this bound function actually select the pair's "second" member to call the function on? It looks at though it will fail to compile, expecting the applied first argument to be convertible to the UDT Point. -- Steven E. Harris :: seharris@raytheon.com Raytheon :: http://www.raytheon.com

On Monday 05 August 2002 03:40 am, Kamil Burzynski wrote:
for_each( my_map.begin(), my_map.end(), map_fun( &Point::Reset ) );
Boost.Bind can do that: for_each(my_map.begin(), my_map.end(), bind(&Point::Reset, bind(&map<int, Point *>::value_type::second, _1))); So can Boost.Lambda: for_each(my_map.begin(), my_map.end(), bind(&Point::Reset, (&_1)->*&map<int, Point *>::value_type::second)); Doug

Douglas Gregor <gregod@cs.rpi.edu> writes:
Boost.Bind can do that:
for_each(my_map.begin(), my_map.end(), bind(&Point::Reset, bind(&map<int, Point *>::value_type::second, _1)));
So can Boost.Lambda:
for_each(my_map.begin(), my_map.end(), bind(&Point::Reset, (&_1)->*&map<int, Point *>::value_type::second));
Again, I can't see how this can work. "second" is the name of a member, not a function. You need a "pair select functor" to grab the first or second member from a pair. I've written a couple of these to use in conjunction with the projection_iterator adaptor. template <class P> struct select_pair_first : public std::unary_function<P, P::first_type> { inline result_type& operator()(argument_type& arg) const { return arg.first; } inline const result_type& operator()(const argument_type& arg) const { return arg.first; } }; template <class P> struct select_pair_second : public std::unary_function<P, P::second_type> { inline result_type& operator()(argument_type& arg) const { return arg.second; } inline const result_type& operator()(const argument_type& arg) const { return arg.second; } }; -- Steven E. Harris :: seharris@raytheon.com Raytheon :: http://www.raytheon.com

On Wednesday 07 August 2002 02:27 pm, Steven E. Harris wrote:
Douglas Gregor <gregod@cs.rpi.edu> writes:
Boost.Bind can do that:
for_each(my_map.begin(), my_map.end(), bind(&Point::Reset, bind(&map<int, Point *>::value_type::second, _1)));
So can Boost.Lambda:
for_each(my_map.begin(), my_map.end(), bind(&Point::Reset, (&_1)->*&map<int, Point *>::value_type::second));
Again, I can't see how this can work. "second" is the name of a member, not a function. You need a "pair select functor" to grab the first or second member from a pair. I've written a couple of these to use in conjunction with the projection_iterator adaptor.
It works, I promise :) Boost.Bind has an overload that supports pointers to data members by automagically transforming them into unary function objects. Boost.Lamda overloads the ->* operator for pointers to data members. Doug

Douglas Gregor <gregod@cs.rpi.edu> writes:
It works, I promise :)
Boost.Bind has an overload that supports pointers to data members by automagically transforming them into unary function objects.
Wow. What does the syntax look like for deducing when an argument is of type pointer to member variable? -- Steven E. Harris :: seharris@raytheon.com Raytheon :: http://www.raytheon.com
participants (4)
-
Douglas Gregor
-
Kamil Burzynski
-
kthomases
-
Steven E. Harris