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.
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.
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?
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.
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?
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.
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.
[+] [-] lolcraft|16 years ago|reply
Yes, "exploiting" is exactly the word I was looking for. Cool hack, though.
[+] [-] jheriko|16 years ago|reply
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
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
[+] [-] tbrownaw|16 years ago|reply
Also, real currying in C++0x: http://pastebin.com/KRraz7iU
[+] [-] fadmmatt|16 years ago|reply
[](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
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.
[+] [-] teamonkey|16 years ago|reply
http://okmij.org/ftp/cpp-digest/Functional-Cpp.html
[+] [-] stingraycharles|16 years ago|reply
[+] [-] jcl|16 years ago|reply
[+] [-] shin_lao|16 years ago|reply
Nice post nevertheless.
[+] [-] Jach|16 years ago|reply
[+] [-] sorbits|16 years ago|reply
For example you can’t do stuff like:
[+] [-] rbanffy|16 years ago|reply
[+] [-] eru|16 years ago|reply
[+] [-] wrs|16 years ago|reply
[+] [-] confuzatron|16 years ago|reply
[+] [-] dirtyaura|16 years ago|reply
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