Is a functor returned by boost::bind with only bound arguments equivalent to a function that takes no arguments?

122 Views Asked by At

Suppose an API requests, that a function f must have the signature void function(). Does the following code sample satisfy this requirements?

 void func(int i)
 {
       do_something(i);
 }

 int main(int argc, char* argv[])
 {
    int i = 3;
    auto f = boost::bind(func, i);
 }

With other words: does f in this case meets the requirements of the given API?

EDIT: My question was a bit vague on the term "function". For clarification: f shall be function-like, so a simple function or a function object. The concrete case why I was asking this question came up, when I worked with strands in Boost.ASIO. Here, the documentation says for a possible handler for strand::post:

The function object to be called. The executor will make a copy of the handler object as required. The function signature of the function object must be: void function();

So: is f from the above example a function object with the correct signature for strand::post?

2

There are 2 best solutions below

8
273K On

is f from the above example a function object with the correct signature for strand::post

Yes, the functor returned by std::bind is a valid functor to io_service::strand::post. std::bind exists for such purposes, it binds paramaters with values and reduces its count to satisfy requirements of API.

1
Hatted Rooster On

No, because std::bind returns an unspecified object. It is required to have cetain members and methods but that's about it. If the exact return type doesn't matter much then perhaps it meets the requirements, but strictly speaking it's not a function.

Edit:

After clarification in the question the answer is strictly yes as this unspecified object is required to behave the way that strand::post requires.