Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 299
polapts Avatar asked Nov 07 '11 11:11

polapts


People also ask

What is boost Phoenix?

Boost. Phoenix is the most important Boost library for functional programming. While libraries like Boost. Bind or Boost. Lambda provide some support for functional programming, Boost.

What does boost :: bind do?

boost::bind is a generalization of the standard functions std::bind1st and std::bind2nd. It supports arbitrary function objects, functions, function pointers, and member function pointers, and is able to bind any argument to a specific value or route input arguments into arbitrary positions.


2 Answers

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.

like image 128
Ralph Zhang Avatar answered Sep 28 '22 07:09

Ralph Zhang


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.

like image 38
ildjarn Avatar answered Sep 28 '22 06:09

ildjarn