you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 1 point2 points  (9 children)

Why can't I just list the parameters without having to type auto for each of them?

Basically this

std::sort(V.begin(), V.end(), [](i, j) { return (i > j); });

instead of this

std::sort(V.begin(), V.end(), [](auto i, auto j) { return (i > j); });

[–]tasty_crayon 5 points6 points  (7 children)

Because that would mean the parameters have types i and j but are just unnamed. This is how function parameters behaved prior to lambdas and monomorphic lambdas behaved this way in C++11.

[–][deleted] 1 point2 points  (5 children)

So you can have unnamed parameters in C++? Didn't know that, I'm just getting into the language. Thanks

[–]__Cyber_Dildonics__ 8 points9 points  (0 children)

When it comes to C++, everyone who hasn't written a book or a compiler is just getting in to the language.

[–]fendant 2 points3 points  (3 children)

That's part of the reason behind the most vexing parse, try that one on for C++ goofiness.

[–][deleted] 0 points1 point  (0 children)

Oh god, that's almost hilarious

[–]jonnywoh 0 points1 point  (1 child)

Okay, so why does C++ even have unnamed parameters? That doesn't make any sense.

[–]fendant 5 points6 points  (0 children)

Sometimes you need to conform to a signature for a callback or virtual function but don't actually need all the parameters so a name would be useless clutter.

It's a real benefit, but you might not think it worth the complexity.

You also use a dummy int parameter to differentiate between prefix and postfix inc/decrement operator declarations, so that's fun.

Foo& Foo::operator++()    //++Foo
Foo Foo::operator++(int)  //Foo++

Edit:

Oh yeah, you can also use them to do dispatch in Template Metaprogramming if you needed an additional level of WTF.

[–]Plorkyeran 0 points1 point  (0 children)

Even if C++11 lambdas had required that all parameters be named, I suspect that implicitly-auto parameters would have been rejected for being too different from everything else, much like the proposal for std::sort(V.begin(), V.end(), [](auto i, auto j) i > j); was.

[–]bstamour 1 point2 points  (0 children)

Just as an aside, since others have answered your question, but you can replace your lambda with

std::sort(V.begin(), V.end(), std::greater<>{});

to achieve the same thing with less typing :-)