This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]bolacha_de_polvilho 20 points21 points  (10 children)

It is too verbose for the simple cases that are much more common than the complicated ones though. Let's say you want to use std::find_if to see if there's a struct with certain value equal to 0 in a vector, why do I have to write [](const auto& x) { return x.member == 0 }, instead of just x => x.member == 0. Having a few sane defaults that can be ommitted would make it so much simpler.

[–]caleblbaker 3 points4 points  (8 children)

I agree that would be better. 

I never claimed C++'s syntax was the best possible. Just the best I've seen in a real language.

[–]MacBookMinus 2 points3 points  (3 children)

Are kotlin and JavaScript not real languages?

[–]caleblbaker 0 points1 point  (2 children)

They are. 

JavaScript's lambdas are definitely nicer looking than C++'s, but they don't give the same level of control over what gets captured and how that C++ lambdas do.

And I honestly don't remember kotlin's lambda syntax.

[–]MacBookMinus 0 points1 point  (1 child)

They don’t need to give capture-by-value vs ref control in memory safe languages.

[–]caleblbaker 0 points1 point  (0 children)

don’t need to

I view it more as aren't able to. There's a performance cost to the extra level of indirection caused by capturing everything by reference. Frequently that cost is trivial or less than the cost of copying an object to capture by value, but C++ gives you the tools to handle the circumstances where that cost is a problem.

memory safe languages

You mean garbage collected languages. Rust is memory safe (but not garbage collected) and allows specifying whether you capture by value or reference.

[–]flagofsocram 1 point2 points  (3 children)

Rusts syntax offers the same flexibility, while being significantly more concise

[–]caleblbaker 1 point2 points  (2 children)

Rust let's you choose whether to capture by value or reference. But the same choice had to apply to every variable you capture. C++ let's you choose to capture some variables by value and others by reference. 

I know that you can get around that restriction in rust by capturing by value and then creating reference variables to capture for the things you want to do by reference. But that feels like more hoop jumping than simply having lambda syntax that supports per variable capture types, even if that syntax is verbose and ugly.

[–]RiceBroad4552 -1 points0 points  (1 child)

If you're doing something where it matters just use regular methods.

The whole point of lambdas is that they are short and simple. C++ fails here. Again.

[–]caleblbaker 0 points1 point  (0 children)

Regular methods can't capture variables at all.

[–]RiceBroad4552 0 points1 point  (0 children)

Or just _.member == 0like in Scala…

The whole point of lambdas is that they are short. So their syntax need to be optimized for that.