3

On Page 175 Paragraph 1 of Effective C++ Meyers has this to say about generalized functors and binding:

I find what tr1::function lets you do so amazing, it makes me tingle all over. If you're not tingling , it may be because you're staring at the definition of ... and wondering what's going on with the ....

And I agree with him on bind and function. About lambda, Well I understand what lambda does and how it does it, but could someone post a book style mind-blowing snippet or a verbal outline of why lambda is supposed to (in Meyers' terminology) blow my socks off ? I ask because each area of C++ where the placeholder syntax is used seems like a hack to me (yes, I know enough about the functional method, so please no basics), I agree with the way it's used in bind and MPL; However, in the case of lambda I just want it justified so I can decide weather I should enter it into my repertoire.

-- edit --

This SO answer mentions the inlined creation of a functor using just placedholder syntax, he mentions advanced usage, and this is probably what I am after... in advanced usage is it still just inlined creation of functors ?

Community
  • 1
  • 1
Hassan Syed
  • 20,075
  • 11
  • 87
  • 171
  • 3
    Did you read the BLL documentation? There are tons of examples that blew my mind off the first time I read it. E.g: `sort(a, b, _1 < _2);`. And then search SO. – dirkgently Jan 30 '10 at 10:50
  • (+1) Yep, I did (at least the first 10 pages) -- and I understand that form of usage. Is this it though ? – Hassan Syed Jan 30 '10 at 10:54
  • 3
    Lambdas are all about inline creation of anonymous functions and Boost.Lambda gave you tools to do that before C++0x lambdas inclusion was even discussed - so i wouldn't say *"thats it?"*. *(hey, don't claim my +1 ;)* – Georg Fritzsche Jan 30 '10 at 10:59
  • 1
    BB and BLL evolved separately, hence the overlap. See http://www.boost.org/doc/libs/1_41_0/doc/html/lambda/s08.html -- that should clear any confusion. – dirkgently Jan 30 '10 at 11:03
  • Thanks guys: you should have made answers though =D, but it would have been hard to accept any of the three answers. I think I will use lambda for the simple stuff -- I was only reading the documentation because I was following the reference from spirit. I wish they would hard-wire functional programming into the language -- including spirit brings my compiler to it's knees. – Hassan Syed Jan 30 '10 at 11:19
  • Not sure what you mean by hard-wiring, but with the next standard a big chunk of Boost will be shipped to you automagically. The lambda syntax is a bit of a horror though: for_each(a.begin(), a.end(), [&sum](int x) {sum += x;}); -- so beware. Try precompiling the boost headers -- that ought to give you joy. – dirkgently Jan 30 '10 at 12:26
  • @dirkgently thanks -- I would have thought pre-compilation didn't bother with template code. I've got most of the headers I need to write grammars using the rule approach in spirit precompiled.... Under Visual Studio the pch file takes 128 MB of space. What I meant by hardwired -- is that the language should allow TMP constructs to be represented so that the compiler doesn't have to generate 128 MB worth of internal data structures. – Hassan Syed Feb 01 '10 at 12:58

2 Answers2

4

Based on the comments left above, and the link in the question, the following is the answer I accept (community wiki) :

  1. Boost.Lambda fills the purpose of inline functor creation (that's the term I like). This functionality can be filled by Function + Bind, but it is more verbose than it needs to be, and for simple functors this is unnecessary — e.g., the sort shown in the comments above.

  2. There is obviously semantic overlap between the Function-Bind pair and Lambda — this is a historical artifact, and because Lambda has its raison d'être, it exists in Boost.

Rob Kennedy
  • 161,384
  • 21
  • 275
  • 467
Hassan Syed
  • 20,075
  • 11
  • 87
  • 171
0

What is "cool" about it is that, as with boost foreach and boost parameter, injects/extends syntax into C++ which is not in the language, ie it emulates anonymous functions directly as parameters.

Viktor Sehr
  • 12,825
  • 5
  • 58
  • 90
  • 1
    Well, the alternative would be to write your own compiler with your own invented language. – Viktor Sehr Jan 31 '10 at 15:00
  • You're looking at this the wrong way ima, it's not why use C++ at all, it's why isn't this part of the C++ syntax yet. The C++0x lambda expressions are really cool, but they're only just a small step towards compiler-supported iterators and iterating over them. – Blindy Apr 15 '10 at 05:31