Not grokking boost::bind

Hey, all. Not sure if this should go here or in Beginners. It's probably already been answered, but a quick search found nothing.


Suppose I have the following function:
1
2
3
4
int HandleCallback(int(*callback)(string))
{
	return callback(string("he;lp"));
}


Further suppose I have a bunch of other functions of various arities taking all sorts of args, including at least one string. Say,
1
2
int SomeFunction(string, int, bool);
int SomeOtherFunction(string, int);


stuff like that. Is there a simple way of taking those functions, binding every arg except the string to them, and then passing them to HandleCallback()? Or doing something similar with functors? Is this at all what boost::bind was intended for, or am I looking for something else entirely?
Last edited on
Yes, but you will need to change the signature of HandleCallback.

1
2
3
4
5
6
7
8
9
10
11
int HandleCallback( const boost::function<int(std::string)>& fn ) {
    return fn( "he;lp" );
}

int SomeFunction( std::string, int, bool ) { /* ... */ }
int SomeOtherFunction( std::string, int ) { /* ... */ }

int main() {
    HandleCallback( boost::bind( &SomeFunction, _1, 42, true ) );
    HandleCallback( boost::bind( &SomeOtherFunction, _1, 42 ) );
}


EDIT: Above calls SomeFunction() with "he;lp" [from HandleCallback], 42 and true.
Last edited on
Nifty. Thanks for the quick reply.
Topic archived. No new replies allowed.