|
![](/i/fill.gif) |
Darren New <dne### [at] san rr com> wrote:
> That's why I asked why a lambda in C++ is better than just coding the body
> in the for loop - I don't see that it gains you anything, and I was
> wondering what you'd code differently.
As I already mentioned, the for_each() was just a simple example. There
are many other situations where a for-loop won't do. std::sort() is a good
example.
You could, of course, create the same effect as with the lambda function
by other means, but it would be laborious and cumbersome (eg. creating a
struct of pointers to all the local variables you want visible
inside the comparator function, and implementing the comparator inside
that struct, and then instantiating the struct and giving it to std::sort),
and most probably the compiler wouldn't even be able to optimize it as well.
The lambda functionality makes this a lot more convenient, and probably
also more efficient.
> I 100% honestly think you're taking things personally that aren't being
> personally directed at you.
You still know that I get trolled when someone writes such mocking words
about C++ as you have done.
> I haven't said you're foolish to use C++ or
> anything else directed at you. I simply said C++ sucks in certain ways, and
> it seems counter-intuitive to me why people as smart as those standardizing
> C++ would add features like lambdas with no obvious benefit compared to the
> pitfalls you have to avoid when you use them.
I really can't understand why you don't see the benefits.
One could argue the same thing about almost any convenience feature.
For example, one could argue that templates are useless because you can
do everything that can be done with templates by other means. Of course
you can. You can write asm and get the same functionality and be done
with it. However, your asm will certainly not be as clear and maintainable,
and certainly enormously longer than high-quality template code can be.
Templates in C++ are very useful for many reasons. For instance, they
help avoiding code repetition when the same thing has to be done with
several types. They also help the compiler create more efficient code.
Templates also help abstracting your code so that it will become more
easy to maintain in the future. There are tons of reasons why templates
are a useful convenience feature, even if, technically speaking, you could
achieve the same end results without them (after all, C has been doing that
for quite a long time).
Templates are in fact so powerful that entire books have been written
about them.
Personally I see C++ lambda functions (in conjunction with things like
auto variables) having the same kind of potential.
--
- Warp
Post a reply to this message
|
![](/i/fill.gif) |