Boost logo

Boost Users :

From: Douglas Paul Gregor (gregod_at_[hidden])
Date: 2003-11-21 13:11:34


On Fri, 21 Nov 2003, Bruce Johnston wrote:

> I'm somewhat new to Boost, but pretty familiar with STL and Loki. I'm a bit
> puzzled by the wide array of similar-sounding libraries in Boost for dealing
> with function objects: Lambda, Function, Functional, and Bind/Mem_fn. Why
> are there so many of these libraries and under what circumstances would I
> want to choose one over the other? Thanks.

Good question... some guidelines

1) Functional provides some fixes and enhancements to the classes in the
standard <functional> header, making them less cumbersome to use. I
personally haven't needed this library in my own work.

2) Mem_fn is similar to Functional in that it provides fixes for the
standard mem_fun. If you're running into the limitations of std::mem_fun
(i.e, need more parameters, need to use smart pointers, etc, etc), use
Mem_fn.

3) Function is completely different from any of the other libraries
mention. It implements the Bridge pattern, mimicking function pointers in
syntax and intent. Function is great for implementing callbacks, because
it is essentially a function pointer that can call any type of function
object.

4) The choice between Bind and Lambda is a tough one. Both libraries
help you compose function objects on-the-fly. First of all, if you
need portability to broken compilers, you can't use Lambda: it requires a
very conforming compiler to be usable. If you are graced with good
compilers, you should consider using Lambda: it offers all of the features
of Bind, but is much more powerful and allows more concise, readable
syntax.

For the best bang for your buck, learn function + bind. Both libraries are
easy to use, very portable, and together provide a sound basis for
programming with function objects. Oh, and these two libraries are being
standardized, so you will see the same interfaces cropping up in your
standard library in the (near?) future.

        Doug


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net