Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c++

bind

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.

like image 775
romeovs Avatar asked May 21 '12 20:05

romeovs


People also ask

What is the use of bind in C++?

std::bind is a standard function object. std::bind function behaves as a Functional Adaptor. It returns a function object as an output on taking a function as an input. The Returned output function object is with one or more of the arguments of passed input function bound.

What is the binding of a function?

The binding of function is directed by the number and position of placeholders. This article is contributed by Manjeet Singh. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to [email protected].

What is the use of placeholder object in bind() function?

These placeholders’ objects are used as arguments of bind () function. And they are stored within the generated function object. Each nth unbound argument replaces the corresponding placeholder (_n) when the function object is called with unbound arguments.

How to pass arguments from one function to another?

Basically, you need to declare the argument as a pointer to a function with the same signature, and then pass a pointer to the target function. Both excellent examples. Thank you kindly.


2 Answers

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.

like image 89
K-ballo Avatar answered Sep 22 '22 19:09

K-ballo


Well, if you know at compile time, what you have to bind c with, you could define a new function

int c_bound(int a, int b) {
   return c(a,b,some_value);
}

That's obviously not a generic solution but might solve your current problem. Otherwise K-ballo's solution seems to be the only easy generic one. However, that requires you to be able to change the signature of func. If you really have an API that you can't touch the signature, and you still need to bind an argument AND if the above solution doesn't solve your specific case: (Caution, overkill ahead) I've always wanted to use an LLVM based solution to compile a function at runtime and pass its address in such situations.

like image 35
enobayram Avatar answered Sep 21 '22 19:09

enobayram