>Funny to see that claiming that the following code is readable stirred up quite a few comments.
std::transform( list.begin(), list.end(), std::back_inserter( res ), std::bind2nd( std::plus<int>(), 42 ) );
Now, there are basically three points that you guys like to point out:
Try another language – the short answer, no. The longer answer, I like C/C++, much because I’ve grown to know them, but also because I can see the actual machine code that the code I write will produce. This, to me as an electronics guy, is really neat. It gives me a sense of control.
Also, I’ve tried Haskell and Lisp (and Excel, if you want another functional language), and I like the concept, but creating something large and actually useful using these tools… I don’t know. I’m sure that you can point out a million examples, but… nah. Not for me. At least not for proper work.
C++0x can do this better – but why change a winning concept. Lambda expressions does not belong in C++. The C-family of languages contains imperative languages, not functional ones. Call me old, call me granddad, but I like it the way it is. If I could go back in time, I’d rather spend my time pushing MS to improve their implementation than changing the specs.
You call that readability? – yes I do, and seeing examples from other languages, I do so even more. The trick is to read code from the right direction. For example, compare reading an ordinary mathematical expression to reading it in RPN. Neither is fun, but as soon as you know how to approach it, both are simple.
I try to read C/C++ from the inside out. Take, the troublesome expression that started this. I would start with the arguments. The first two are simple – just limiting the input data. The third is a bit more tricky: std::back_inserter( res ). Try starting from the inside, what is res? A list of integers. What could a back_inserter do to a list? Perhaps insert stuff at the back of it.
Continuing with the operation: std::bind2nd( std::plus<int>(), 42 ). Again, starting from the inside, plus probably adds two arguments together, integers judging from the template specialization. So, bind2nd? Coming from an engineering background (and having done this a couple of times) I thing that it is quite clear that it binds (locks) the second argument to a value, in this case 42. I can admit that this is not 100% clear if you’re not familiar with the topic.
So, taking one last step out I find myself looking at the word transform. So, a list of items is transformed into another list. Again, not too hard to grasp, but you might want to look up the details in an STL reference. Speaking of such, SGI hosts are really good STL reference. So, for the curios readers, given these links I think that you also can call the transform expression readable: