composition and arguement binding: what to use: boost::bind vsboost::mpl vs boost::lambda
I've become rather confused about the exact relationship between these libraries. In particular, boost::mpl seems to contain placeholders and lambda templates just like boost::bind and boost::lambda. That leds me to wonder if there would be any point to using boost::bind or boost::lambda if you are using boost::mpl? In fact, if boost::mpl is a functional superset of the other two libraries, why not drop those libraries from boost altogether and eliminate the confusion? Rob.
On Tue, 15 Mar 2005 11:56:31 -0500, Robert Mathews
I've become rather confused about the exact relationship between these libraries. In particular, boost::mpl seems to contain placeholders and lambda templates just like boost::bind and boost::lambda.
That leds me to wonder if there would be any point to using boost::bind or boost::lambda if you are using boost::mpl? In fact, if boost::mpl is a functional superset of the other two libraries, why not drop those libraries from boost altogether and eliminate the confusion?
Rob.
boost::mpl performs binding of meta-functions (i.e. binding within the compile-time domain), whereas bind and lambda perform binding of functions and function objects (i.e. binding within the run-time domain). As far as I can see, Boost.Bind is effectively a subset of Boost.Lambda (I may well be wrong, though!). However, if you're using an older compiler (such as MSVC6), then using Boost.Lambda can be problematic, whereas Boost.Bind works fine. HTH Stuart Dootson
Stuart Dootson
On Tue, 15 Mar 2005 11:56:31 -0500, Robert Mathews
wrote: I've become rather confused about the exact relationship between these libraries. In particular, boost::mpl seems to contain placeholders and lambda templates just like boost::bind and boost::lambda.
That leds me to wonder if there would be any point to using boost::bind or boost::lambda if you are using boost::mpl? In fact, if boost::mpl is a functional superset of the other two libraries, why not drop those libraries from boost altogether and eliminate the confusion?
Rob.
boost::mpl performs binding of meta-functions (i.e. binding within the compile-time domain), whereas bind and lambda perform binding of functions and function objects (i.e. binding within the run-time domain).
As far as I can see, Boost.Bind is effectively a subset of Boost.Lambda (I may well be wrong, though!). However, if you're using an older compiler (such as MSVC6), then using Boost.Lambda can be problematic, whereas Boost.Bind works fine.
And unless Boost.Lambda has been updated since I looked, there are some nice things that Boost.Bind handles automatically, like calling through smart pointers, that aren't quite so easy with Boost.Lambda. -- Dave Abrahams Boost Consulting www.boost-consulting.com
David Abrahams wrote:
And unless Boost.Lambda has been updated since I looked, there are some nice things that Boost.Bind handles automatically, like calling through smart pointers, that aren't quite so easy with Boost.Lambda.
I tried recently to use containers of smart pointers (boost::shared_ptr) and ran into a real mess of error messages (VC++ 7.1), even though I was doing a 'simple' std::find_if() with a _1 == value test. When I had simple pointers Boost.Lambda worked nice and cleanly, but switching to shared_ptr, was almost impossible to work out what was going on, I simply switched to a few composed boost::bind()s and it worked first time, so I have to agree here that Boost.Bind wins out on this simple use, even though it looses the nice syntax of Boost.Lambda. Subsequently, the containers now contain std::pair's to shared_ptrs, so I have even more composition going on, which for a one off 'function' is getting messy, with Lambda it should be much simpler so I'd certainly like to know if I missed something with this. Kevin -- | Kevin Wheatley, Cinesite (Europe) Ltd | Nobody thinks this | | Senior Technology | My employer for certain | | And Network Systems Architect | Not even myself |
Kevin Wheatley wrote:
Subsequently, the containers now contain std::pair's to shared_ptrs, so I have even more composition going on, which for a one off 'function' is getting messy, with Lambda it should be much simpler so I'd certainly like to know if I missed something with this.
Extracting a member of a pair is equally painful with Bind and Lambda:
bind(&Pair::second, _1). Lambda also supports _1->*&Pair::second for
ordinary pointers, but not for shared_ptr, where you'll need something like
&*_1->*&Pair::second.
The current CVS bind now supports
!bind(...)
bind(...) == value
bind(...) == _1
bind(...) == bind(...)
and similarly for !=, <, <=, >, >=. You may be able to avoid some of the
compositions.
Other notable differences between Bind and the bind subset of Lambda are the
number of supported placeholders and support for function<>::contains. They
also differ in their handling of the first argument when it's a nested
lambda/bind expression or a placeholder; Lambda can do bind( _1, _2 ), Bind
cannot. Bind also respects its const qualifier so that when a non-const
bind( f, 1 ) is called, the non-const operator() of f is called and it can
mutate the stored copy of 1. Most of these are of no concern for ordinary
use. Well, maybe except for cool examples:
#include
Peter Dimov wrote:
Kevin Wheatley wrote:
Subsequently, the containers now contain std::pair's to shared_ptrs, so I have even more composition going on, which for a one off 'function' is getting messy, with Lambda it should be much simpler so I'd certainly like to know if I missed something with this.
Extracting a member of a pair is equally painful with Bind and Lambda: bind(&Pair::second, _1). Lambda also supports _1->*&Pair::second for ordinary pointers, but not for shared_ptr, where you'll need something like &*_1->*&Pair::second.
Peter,
Thanks for the reply. It's all starting to look rather like Perl at
this point and as such my inbuild parser is failing to parse what that
does at a glance (can't work out what set of rules I've got to use
:-), so it might be worth avoiding just so that mortals can simply see
what is going on!
I guess if Lambda understood about smart pointers in a similar way to
Bind some of this syntax could be simplfied.
see attatched for what I'd like to do.
Kevin
--
| Kevin Wheatley, Cinesite (Europe) Ltd | Nobody thinks this |
| Senior Technology | My employer for certain |
| And Network Systems Architect | Not even myself |
#include <vector>
#include <algorithm>
#include <iostream>
#include <cstddef>
#include
"Kevin Wheatley"
David Abrahams wrote:
And unless Boost.Lambda has been updated since I looked, there are some nice things that Boost.Bind handles automatically, like calling through smart pointers, that aren't quite so easy with Boost.Lambda.
I tried recently to use containers of smart pointers (boost::shared_ptr) and ran into a real mess of error messages (VC++ 7.1), even though I was doing a 'simple' std::find_if() with a _1 == value test.
When I had simple pointers Boost.Lambda worked nice and cleanly, but switching to shared_ptr, was almost impossible to work out what was going on, I simply switched to a few composed boost::bind()s and it worked first time, so I have to agree here that Boost.Bind wins out on this simple use, even though it looses the nice syntax of Boost.Lambda.
FWIW, my mileage with Boost.Lambda was much the same - the simple cases worked fine, but anything with smart pointers didn't work well. On other hand, I found that boost::bind worked simply and cleanly, and was a vast improvement over the stl memfun/memfun1/etc family of functions. (I'm also using VC++ 7.1)
Subsequently, the containers now contain std::pair's to shared_ptrs, so I have even more composition going on, which for a one off 'function' is getting messy, with Lambda it should be much simpler so I'd certainly like to know if I missed something with this.
Kevin
-- | Kevin Wheatley, Cinesite (Europe) Ltd | Nobody thinks this | | Senior Technology | My employer for certain | | And Network Systems Architect | Not even myself |
participants (5)
-
David Abrahams
-
Kevin Wheatley
-
Peter Dimov
-
Robert Mathews
-
Stuart Dootson