Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can intermixing std::'s and boost::'s ::bind and ::function cause problems?

Tags:

c++

std

c++11

boost

I think the answer to this is no, but I just want to be sure.

If I have a std::function as a parameter in a function, is there any problem with passing in a boost::bind and vice-versa?

edit:

I discovered that the placeholders used by boost::bind are imported directly into the namespace when you include boost\bind.h, and they are incompatible with std::bind. For std::bind you have to refer to the placeholders explicit, like so: std::placeholders::_1, or do some other typedef or using magic to make them both available simultaneously.

like image 239
cwm9 Avatar asked Sep 02 '12 11:09

cwm9


People also ask

What does std :: bind do?

std::bind. The function template bind generates a forwarding call wrapper for f . Calling this wrapper is equivalent to invoking f with some of its arguments bound to args .

What is _1 in boost :: bind?

_1 is a placeholder. Boost. Bind defines placeholders from _1 to _9 . These placeholders tell boost::bind() to return a function object that expects as many parameters as the placeholder with the greatest number.

What is the return type of std :: bind?

std::bind return type The return type of std::bind holds a member object of type std::decay<F>::type constructed from std::forward<F>(f), and one object per each of args... , of type std::decay<Arg_i>::type, similarly constructed from std::forward<Arg_i>(arg_i).

What is std :: placeholder?

The std::placeholders namespace contains the placeholder objects [_1, ..., _N] where N is an implementation defined maximum number.


1 Answers

No. The whole purpose of std:: (and boost::) function is that they can accept any function object which can be called with the correct signature- including lambdas, functors, and the result of any kind of binding. They do not care where your function object came from or what type it is.

You can even bind them to each other, although I'm not really sure why you'd want to.

like image 94
Puppy Avatar answered Oct 15 '22 19:10

Puppy