May 26, 2004
Boost Lambda

I'm sure the Boost Lambda Library for C++ has been around for a while, but Tom just showed it to me.

Finally, the following for_each call outputs the sorted content of vp separated by line breaks:

  for_each(vp.begin(), vp.end(), cout << *_1 << "\n");

But wait, it gets much better.

Note that a normal (non-lambda) expression as subexpression of a lambda expression is evaluated immediately. This may cause surprises. For instance, if the previous example is rewritten as

  for_each(vp.begin(), vp.end(), cout << "\n" << *_1);

the subexpression cout << "\n" is evaluated immediately and the effect is to output a single line break, followed by the elements of vp.

I'd love to see the compiler messages you get when you make a typo.

(An old post from Lambda the Ultimate has more information.)

Posted by jjwiseman at May 26, 2004 11:10 AM


Minor nit: the syntax doesn't use a "*" before the placeholder, so:

for_each(vp.begin(), vp.end(), cout

Posted by: Paul Snively on May 28, 2004 03:43 PM

If you think boost::lambda is cool, BTW, you should also check out <> and <>. Functional programming has hit the C++ world big-time.

Posted by: Paul Snively on May 28, 2004 03:53 PM

Wow, phoenix makes C++ like, not TOTALLY suck!

Posted by: David Mercer on May 28, 2004 09:45 PM
Post a comment

Email Address:


Unless you answer this question, your comment will be classified as spam and will not be posted.
(I'll give you a hint: the answer is “lisp”.)


Remember info?