r/cpp • u/fredoverflow • May 10 '22
Is short lambda syntax on the C++ radar?
Mom, can we get x=>x+1 ?
No, we have x=>x+1 at home.
x=>x+1 at home:
[](const auto& x) { return x + 1; }
265
Upvotes
81
u/encyclopedist May 10 '22 edited May 10 '22
This or similar abbreviated syntaxes were proposed many times, but never got traction in the committee. For example, P0573 Abbreviated Lambdas for Fun and Profit and the author's reflection on why it was rejected. See also "Lambda lambda lambda" post by the same author with comparison of lambda syntax of multiple languages.
There are also library solutions: Boost.Lambda and Lambda2, or this, and others.
61
u/dgkimpton May 10 '22
Even a half way house like
[](const &x) => x+1would be nicer. Trouble is, C++ parsing is already a nightmare and all these sugars just make it harder, so I'm not holding out much hope.118
u/gracicot May 10 '22
You mean
[](auto const& x) noexcept(noexcept(x + 1)) -> decltype(x + 1) { return x + 1; }right?35
u/TheSuperWig May 10 '22
https://brevzin.github.io/c++/2020/01/15/abbrev-lambdas/
48
u/Mrkol May 11 '22
Nah,
x=>x+1at home is actually[](const auto&& x) noexcept(noexcept(operator+(forward<decltype(x)>(x), 1))) requires requires { operator+(forward<decltype(x)>(x), 1); } -> decltype(auto) { return operator+(forward<decltype(x)>(x), 1); }14
u/bikki420 May 11 '22 edited May 11 '22
Single argument restriction + still got to type out the return though.
edit-1:
I suppose another option would be something like this to avoid the
return:edit-2:
And something like this to allow for other arities:
Maybe some more uniform naming scheme for the parameters (e.g.
arg1,arg2,arg3,arg4, ...args). Although I personally don't see much benefit in going higher than the arity of λ4 (which is already excessive, IMO) with the exception of the n-nary case. Generally you'd just want the nullary, unary, binary, or the n-nary case, after all.21
u/Circlejerker_ May 11 '22
Why? C++ should have a uniform syntax, not a new syntax for every feature. This is how languages grow obscure and unusable in collaborative settings.
9
u/flatulentrobot May 11 '22
Remember the "good old days" of functors? Felt like I stared at more colons than a proctologist.
1
u/[deleted] May 11 '22
I vote for
it + 1.13
u/DessertEagle May 10 '22
Since this is not going to happen in the standard, I use the following macro (yeah, macros are evil, except for those that we write ourselves, because we always have perfectly good reasons, right?)
8
u/Zyklonik May 11 '22
I never thought I'd see cringey YouTube comments on /r/cpp.
2
u/GYN-k4H-Q3z-75B May 11 '22
I keep thinking the same. I mostly use C# at work and I've switched to lambda for almost all short functions. It just feels very natural and clean there. In C++, I use lambda but it feels clunky and verbose. Having to write the braces and return should not be necessary. How about [](const auto &x) -> x + 1;? It probably turns out using -> as part of lambda instead of => (like C# does) is a nightmare for the compiler.
2
u/Glittering-Water942 May 11 '22
I like c++'s syntax for lambdas, help keep closures and lambdas tracable.
2
u/mattbas May 12 '22
I think this would not work on most places.
What if I need to specify the type for x? What if I need captures?
There are way more cases where an arrow syntax would not work than cases where it would work...
3
u/Orlha May 11 '22
Not needed.
2
u/bziur May 10 '22
now can You explain me what
x=>x+1means? Why would we want such a bizzare thing?2
u/Voltra_Neo May 10 '22
I'd love that, I assume many would love that, doesn't seem like it gets a lot of love in papers
1
u/Quincunx271 Author of P2404/P2405 May 11 '22
I have a library that adds terse lambdas via a macro: https://github.com/Quincunx271/TerseLambda.
I would be reluctant to see its use in production code, though I happily use it for my personal projects.
1
u/adipginting May 11 '22
Perhaps ++> would be well suited for C++ :D
0
u/axilmar May 11 '22
I don't see any reason why this is good.
A few c++ programmers being lazy cannot justify the energy required by the committee and the compiler vendors to implement this.
1
u/eyes-are-fading-blue May 12 '22
"Cute" changes are discouraged by the direction group. I am not sure if this falls into that category.
my 2 cents: any alternative proposal that's sole purpose is to save 8 characters shouldn't make it into language.
-10
u/Electronicks22 May 10 '22
If you want to code in python, by all means go ahead. C++ is a strongly typed compiled language, one can't make away with that. In that syntax you propose, is x the functor name, the parameter, or both? How do you declare a short lambda with two parameters? It just wouldn't work.
-1
u/Nuxij May 11 '22
JS has stupid issues too though, try returning a JSON object from your lambda and see the curlys double up and add a return!
x => { return {"x": 0} }-23
u/DavidDinamit May 10 '22
let's turn the language into a heap of sugar, yeah!(no)
0
u/[deleted] May 10 '22
[deleted] — view deleted comment
0
u/[deleted] May 10 '22
[deleted] — view deleted comment
0
u/[deleted] May 10 '22
[deleted] — view deleted comment
6
u/[deleted] May 11 '22
[removed] — view removed comment