Difference between boost::bind, boost::lambda::bind and boost::phoenix::bind

1.9k Views Asked by At

I am trying to understand the difference between these different bind approaches. There is a similar question at boost::bind and boost::phoenix::bind

But, if anyone can explain this with examples it would be great. Also is it true that boost::phoenix is a superset of boost::bind, booost::lambda libraries?

2

There are 2 best solutions below

2
On

I think the story is (though I'm not old enough to tell the whole story), boost::bind was first created to replace the hard-to-use bind1st/bind2nd in C++98, and it achieves its goal and now part of C++11. But also as last 10 years saw the rise of functional programming style in C++, boost::lambda pushes it so far (at the time it was created) that it supports a reasonably wide set of functional constructs with pure library approach in C++.

And then as I know from the news group, the author of boost::lambda and boost::phoenix try to combine the two libraries as they deal with virtually the same problem. I guess that was the beautifully designed boost::phoenix2

And then there comes boost::proto, which is a libary for writing expression templates, or I'd say it's a meta-library. So the phoenix nirvana again, reborn itself on boost::proto, then we see phoenix3. I think phoenix3 is the most powerful among all above.

On the other hand, C++11 adds language support for lambda expression, which I personally find very useful and handy. The only drawback is it's not polymorphic (while phoenix3 allows creating polymorphic function objects).

As a conclusion that I come with personal experience, C++11 lambda expression is the choice for daily job, if available. It's handy, clear and compile-time friendly. Phoenix3 is polymophic, very powerful, very cool, with the drawback of long compile-time.

3
On

But, if anyone can explain this with examples it would be great.

Examples of what? They're different implementations of the same concept.

Here's what's actually important:

  1. Boost.Lambda has been officially deprecated since Boost.Phoenix was released as a standalone library (and of course boost::lambda::bind along with that).
  2. The implementation of boost::bind is going to be replaced with that of boost::phoenix::bind in the future. The only reason it hasn't been replaced already is that boost::bind supports/has workarounds for older (read: broken) compilers e.g. MSVC6, whereas Boost.Phoenix strictly requires a C++03-compliant compiler.

Combine these two facts and it becomes clear that the only real candidate for use in new code is boost::phoenix::bind.

Also is it true that boost::phoenix is a superset of boost::bind, booost::lambda libraries?

Yes, this is correct.