I had a case today where I needed a functor that was a bound member
function. Basically:

class MyClass {

...bug free code omitted...

void PrintMe( int x ) {
printf( "%d\n", x );
}

void A_Function() {
...
std::vector<int> myList;
...
std::for_each(
myList.begin(),
myList.end(),
bound_mem_fun( *this, PrintMe ); // bound_mem_fun is made up
);
}

Am I correct in my conclusion that there is no such adapter in the standard
library? It looks like there is one in Boost, and it only took about 8
lines of code to implement it, but I don't want to reimplement it if there
is one built in that I missed.
--
- Tim Roberts, timr@probo.com
Providenza & Boekelheide, Inc.

Re: Functor adaptor question by adebaene

adebaene
Thu Jun 08 03:26:04 CDT 2006


Tim Roberts a =E9crit :

> I had a case today where I needed a functor that was a bound member
> function. Basically:
>
> class MyClass {
>
> ...bug free code omitted...
>
> void PrintMe( int x ) {
> printf( "%d\n", x );
> }
>
> void A_Function() {
> ...
> std::vector<int> myList;
> ...
> std::for_each(
> myList.begin(),
> myList.end(),
> bound_mem_fun( *this, PrintMe ); // bound_mem_fun is made up
> );
> }
>
> Am I correct in my conclusion that there is no such adapter in the standa=
rd
> library? It looks like there is one in Boost, and it only took about 8
> lines of code to implement it, but I don't want to reimplement it if there
> is one built in that I missed.

You can do it with the standard library "mem_fun" facility, but it is
really clumsy, and you should prefer boost::bind if you can (btw, I
think boost::bind has been intergrated in CR1, so it will be part of
"official" C++ quite soon).

The STL version read like this:

for_each(myList.begin(), myList.end(),
bind1st (mem_fun1<void, MyClass ,int>(&MyClass::PrintMe),
this));



The boost version reads like this (ans it takes only 1 line!) :

#include <boost/bind.hpp>
using namespace boost;
.=2E..
for_each(myList.begin(), myList.end(),
bind(&MyClass::PrintMe, this, _1));

Arnaud
MVP - VC


Re: Functor adaptor question by Alex

Alex
Thu Jun 08 05:20:36 CDT 2006

adebaene@club-internet.fr wrote:
> The STL version read like this:
>
> for_each(myList.begin(), myList.end(),
> bind1st (mem_fun1<void, MyClass, int>(
> &MyClass::PrintMe), this));

You can make it little bit shorter by sparing template
parameters and using mem_fun instead:

for_each(myList.begin(), myList.end(),
bind1st(mem_fun(&MyClass::PrintMe), this));


Alex



Re: Functor adaptor question by Tim

Tim
Fri Jun 09 23:59:41 CDT 2006

adebaene@club-internet.fr wrote:
>
>Tim Roberts a écrit :
>
>> I had a case today where I needed a functor that was a bound member
>> function. Basically:
>>...
>> Am I correct in my conclusion that there is no such adapter in the standard
>> library? It looks like there is one in Boost, and it only took about 8
>> lines of code to implement it, but I don't want to reimplement it if there
>> is one built in that I missed.
>
>You can do it with the standard library "mem_fun" facility, but it is
>really clumsy, and you should prefer boost::bind if you can (btw, I
>think boost::bind has been intergrated in CR1, so it will be part of
>"official" C++ quite soon).
>
>The STL version read like this:
>
>for_each(myList.begin(), myList.end(),
> bind1st (mem_fun1<void, MyClass ,int>(&MyClass::PrintMe), this));

That's not so bad. It's able to deduce the types, so this works fine:

for_each(myList.begin(), myList.end(),
bind1st( mem_fun1( &MyClass::PrintMe ), this));

>The boost version reads like this (ans it takes only 1 line!) :
>
>#include <boost/bind.hpp>
>using namespace boost;
>....
>for_each(myList.begin(), myList.end(),
> bind(&MyClass::PrintMe, this, _1));

I agree that's cleaner, and I probably need to use boost a lot more, but I
can live with the bind1st/mem_fun1 solution for now.

Thank you sincerely for taking the time to read my lengthy description and
propose a solution. I just don't have enough experience yet to
automatically recognize the binder/adapter combinations on sight.
--
- Tim Roberts, timr@probo.com
Providenza & Boekelheide, Inc.