Difference between boost::bind, boost::lambda::bind and boost::phoenix::bind
Asked Answered
S

2

12

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?

Surculose answered 7/11, 2011 at 11:27 Comment(3)
C++11 also has lamba built-in to the language. Latest Visual Studio and g++ support it. You should add that to the mix !!!Fayum
I think the point here is to differentiate various approaches provided by boost for binding, not enlisting all lambdas available.Surculose
The answer to the duplicate you linked to perfectly answers this question, too.Antoniettaantonin
A
10

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.

Alek answered 9/11, 2011 at 1:29 Comment(2)
Good answer, but two small nits. :-] 1) The author of Boost.Lambda has made no direct contributions to any version of Phoenix, only influence. 2) Phoenix v3's compile times are actually faster than Boost.Lambda's, if you only include the minimal required headers.Tychon
@RalphZhang, C++14 has now polymorphic lambdasDredi
T
1

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.

Tychon answered 9/11, 2011 at 1:43 Comment(3)
You say, "The implementation of boost::bind is going to be replaced with that of boost::phoenix::bind in the future." That's actually far from certain. Boost::bind is very limited in functionality, by design. Significant thought and discussion would be needed first before it could be replaced with something much larger in scope like Phoenix.Cissy
@Eric : That statement was based on conversations on the Boost dev ML; I don't have a link handy, but my recollection was that Beman and Thomas were in favor of the replacement (this was during Phx v3 development, and the exact topic was Boost-wide placeholder unification), and that Peter had no objections. It's true, though, that even with Phx v3 having been out for some time, there appears to be no movement in this direction. :-[Tychon
Boost-wide placeholder unification is something I've been meaning to address. Maybe I can give this matter some attention after BoostCon.Cissy

© 2022 - 2024 — McMap. All rights reserved.