C++ bind function for use as argument of other function

bind, c++

Solution

You can't do that. You would have to modify `func` to take a function-object first. Something like:

int func( int a, std::function< int(int, int) > b )
{
    return b( a, rand() );
}

In fact, there is no need for `b` to be an `std::function`, it could be templated instead:

template< typename T >
int func( int a, T b )
{
    return b( a, rand() );
}

but I would stick with the `std::function` version for clarity and somewhat less convoluted compiler output on errors.

Then you would be able to do something like:

int i = func( 10, std::bind( &c, _1, _2, some-value ) );

Note all this is C++11, but you can do it in C++03 using Boost.

Problem

I have a function that requires a function pointer as argument: ``` int func(int a, int (*b)(int, int)) { return b(a,1); } ``` Now I want to use a certain function that has three arguments in this function: ``` int c(int, int, int) { // ... } ``` How can I bind the first argument of `c` so that I'm able to do: ``` int i = func(10, c_bound); ``` I've been looking at `std::bind1st` but I cannot seem to figure it out. It doesn't return a function pointer right? I have full freedom to adapt `func` so any changes of approach are possible. Althoug I would like for the user of my code to be able to define their own `c`... note that the above is a ferocious simplification of the actual functions I'm using. The project sadly requires `C++98`.

Original source