tags:

views:

692

answers:

3
+4  A: 

The "STL solution" would be to write your own binder... that's why they created the powerful boost::bind.

UncleZeiv
powerfool? :p
jalf
eh eh :) fixed!
UncleZeiv
+12  A: 

A reliable fallback when the bind-syntax gets too weird is to define your own functor:

struct callDoSomething {
  void operator()(const X* x){
    StaticFuncClass::doSomething(x->getName(), funcReturningString());
  }
};

for_each(ctr.begin(), ctr.end(), callDoSomething());

This is more or less what the bind functions do behind the scenes anyway.

jalf
+1 Yes, I had thought about that too. I was just wondering if there was an inline way to do it like binding only 1 of the 2 using bind1st or bind2nd.
RC
No need for a constructor, though.
xtofl
It might be possible with the STL bind functions, but it gets a bit too hairy for me. I'd prefer the functor approach. Of course in C++0x you could define it inline with a lambda
jalf
@xtofl: doh, you're right. I'm not sure I want to edit it though. Seems to get CW'ed awfully fast when I do that. ;)
jalf
And you can make it even smaller by using a `void callDoSomething( const X* x )` function. (PS: what's CW?)
xtofl
community wiki. Happens if you edit your post too often. And I already made a few edits to this one. ;)As for making it a function, true, but then it'd be harder for the compiler to inline.
jalf
+3  A: 

You can either create a local functor structure, which can be inlined by the compiler (as Jalf showed), or use a simple function:

void myFunc( const X* x ) { 
    StaticFuncClass::doSomething(x->getName(), funcrReturningString() ); 
}

for_each( c.begin(), c.end(), myFunc );
xtofl
jalf
You're right. It isn't necessary. Bad habit, I'm afraid.
xtofl