top | item 1262007

Lambda-style anonymous functions for C++ in less than 500 lines of code

111 points| adg001 | 16 years ago |matt.might.net | reply

40 comments

order
[+] lolcraft|16 years ago|reply
To pull this off, the implementation exploits templates, operator overloading and function pointers (but, remarkably, not a single macro) to construct a type-safe domain-specific embedded language.

Yes, "exploiting" is exactly the word I was looking for. Cool hack, though.

[+] jheriko|16 years ago|reply
This is interesting, but I'm not upvoting it... I wrote something similar for dealing with calculus on tensor fields a few years back and most compilers I've written use something similar for dealing with expressions.

This is different though, it doesn't let you solve any new problems that C++ can't already solve, and it also happens to produce slower expressions than, e.g. a function object or a function being pointed to.

If you want lambdas (which let you solve exactly no problems you can't already solve with C++) then use a language that supports them instead of shoehorning them in badly.

I'd recommend Haskell...

[+] fadmmatt|16 years ago|reply
(Article author here.)

I'd love to use Haskell, but I'm helping out on an exascale DoE project, and C++ is all we're allowed to use. Ugh.

Of course, lambdas don't make C++ more powerful, but that's just reductio ad Turing tar-pit.

You can turn that argument around, too: there's no problem you can solve with C++ that you can't solve with lambdas alone:

http://matt.might.net/articles/church-encodings-demo-in-sche...

If programming languages were about computational power, we would have stopped with Fortran.

Programming languages are about the freedom of expressions.

[+] growingconcern|16 years ago|reply
This doesn't make sense. You're saying adding lambdas doesn't help you solve any problems that can't already be solved in C++, but if you want to solve some of those problems you should use a language that supports lambdas?
[+] tbrownaw|16 years ago|reply
Very cool, but fortunately soon to be unnecessary: http://en.wikipedia.org/wiki/C%2B%2B0x#Lambda_functions_and_...

Also, real currying in C++0x: http://pastebin.com/KRraz7iU

[+] fadmmatt|16 years ago|reply
Neat; but C++0x does this:

[](int x, int y) { return x + y; }

instead of this:

lambda<int> (x,y) --> x + y

Ironically, the template hack could be shorter in many cases.

[+] tman|16 years ago|reply
Unfortunate that this is one part of the C++0x g++ project that has yet to get serious. (At least the last time I checked out the g++ experimental code a few months ago.) C++0x lambdas are the one thing that I've been wanting to try out for years.

Instead, we got a lot of (now somewhat wasted) effort on concepts. I like concepts in principle, but too many C++ people saw concepts as a way to write provable code and unreadable library documentation instead of as a way to simplify compiler error messages.

[+] jcl|16 years ago|reply
As written, though, isn't this limited to only multiplication and addition? If you wanted to include, for example, a function call in your anonymous function, wouldn't that require a pile of Var<>-based function overloads?
[+] shin_lao|16 years ago|reply
Or you can use Visual Studio 2010 and use C++ lambdas.

Nice post nevertheless.

[+] Jach|16 years ago|reply
This is so much nicer than Boost's stuff. Quite the exploit but still awesome, it'll probably go into my next side-project.
[+] sorbits|16 years ago|reply
Though it doesn’t work with the standard algorithms.

For example you can’t do stuff like:

    std::transform(first, last, lambda<int> (x) --> 2*x);
[+] rbanffy|16 years ago|reply
Less than 500 lines of C++ code is very little? A lot? I don't know what to make of it.
[+] eru|16 years ago|reply
500 lines is not a lot in C++. Or is it?
[+] wrs|16 years ago|reply
Oh my. It's true what they say about teaching a pig to dance.
[+] confuzatron|16 years ago|reply
Less than 500 lines of code. More than 500 lines of compiler errors if you get a character wrong somewhere. ;)
[+] dirtyaura|16 years ago|reply
Yes. However, in my experience, on a small team, benefits of readable code by using templates to implement DSLs in C++ still outweighs burden of prosaic error messages. You quite quickly learn to pattern match error messages to find the real problem.

We might be bitten this later when new guys and gals join the team. Code is very readable for being C++, but newbies might have hard first few weeks with these error messages.

[+] growingconcern|16 years ago|reply
It's not that hard parsing template compiler errors (though I am looking forward to better errors in C++0x). Are you saying this is a good reason not to use templates at all? With a little practice they start to make sense. I'm sure for novice programmers the first time they see 500+ error messages stemming from a missed semi colon or curly brace (in regular code) it can be pretty bewildering too, but just like with template error messages you just ignore virtually all of it.