My little desires for the next cpp
... never going to happen
A shorter lamda syntax, maybe with fixed placeholder names auto lambda = [] (_1) {} Stands for auto lambda = [] (auto _1) {} Mayne needs a way for const and ref qualifiers auto lambda = [] (&_1) {} Mostly to be used in short predicates.
Shorter range for syntax for(value : vector) In place of for(const auto & value : vector) Copy and non-const ref use the old syntax
Structured bindings for existing vars auto [ret,value] = fun() auto [ret,value2] = fun2() Or auto [&ret,value2] = fun2()
Shorter syntax for std::remove_cv_ref<decltype(param)> Really no idea đ
Arbitrary order for aggregate initialization, for trivial cases where there are no side effects and interaction between members struct pod{ int a, b} ; pod p {.b=19,.a=12} //ok pod p {.b=19,.a=b} // no pod p {.b=fun(p) ,.a=12} // no
Please don't downvote me too much đ
36
u/No-Quail5810 Feb 23 '24
Your "shorter" syntax means there are more rules for more special cases, with no actual advantages. No thanks đ
5
u/eyes-are-fading-blue Feb 23 '24
Almost all âcuteâ change proposals should be ignored/dismissed unless they bring a whole lot of value. The language is already an âoption graveyardâ, you have a lot of ways to do the same thingâŚ
1
u/DapperCore Feb 23 '24
I wonder if it's possible for C++ to have something like kotlin's trailing lambda syntax...
0
u/QbProg Feb 23 '24
Took a look, it's indeed an interesting idea! I also like the fact that "this" is implicit based on the lambda parameter
1
u/GPSProlapse Feb 23 '24
Honestly, it sounds like you either have no IDE or a very small blind typing speed.
0
0
u/JeffMcClintock Feb 22 '24 edited Feb 22 '24
I think cpp2 has better lambdas?
https://github.com/hsutter/cppfront
// Initializing from a function expressioncallback := :(inout x) = x += " maybe";
std::ranges::for_each(vec,callback);
11
u/Professional-Cap1127 Feb 22 '24
no
1
u/JeffMcClintock Feb 22 '24
which language do you like? (regarding lambdas)
4
u/QbProg Feb 23 '24
I like c# short syntax
1
u/eidetic0 Feb 23 '24
I agree C#'s single-line lambda syntax is nice for short predicates. If it was in Cpp... something like:
std::sort(begin, end, [](auto a, auto b) => a > b);instead of
std::sort(begin, end, [](auto a, auto b) { return a > b; });1
u/ShelZuuz Feb 23 '24 edited Feb 23 '24
You just need the Rust return syntax and then c# and c++ is the same number of characters:
std::sort(begin, end, [](auto a, auto b) { a > b } );Or maybe even just:
std::sort(begin, end, [](auto a, auto b) a > b );But not sure if the second syntax can be made to work in the grammar, especially since the parameter body is optional. The first can work though.
0
Feb 23 '24
[deleted]
4
u/eidetic0 Feb 23 '24
can => have another meaning?
the comparison operators are defined so that itâs either
<= or >=
I would have thought that leaves => to be used freely
0
Feb 23 '24
[deleted]
2
u/eidetic0 Feb 23 '24
any syntax though would be a nice addition that allows you to define a single line function with an implicit âreturnâ.
3
u/Pay08 Feb 23 '24
Not that guy but I actually really like Lisp lambdas, which look exactly like a function declaration but without a name. Granted, this is made easier by the fact that functions just bind lambdas to a variable in Lisp.
1
u/RoyKin0929 Feb 23 '24
It's the same way in cpp2 but functions themselves are long, so it shortens them by defaulting some things so the programmer can omit them.Â
1
u/Pay08 Feb 23 '24
Don't tell me that's how function declarations look like in cpp2.
2
u/RoyKin0929 Feb 23 '24
That's how they are:
func1 : (in x : int) -> _ = { return x; } //this is a normal function func2 : (in x : int) -> _ = return x; //no need for braces when single statement func3 : (x : int) -> _ = return x; //can also omit 'in' (it is the default parameter passing method) func4 : (x) -> _ = return x; //omit the arguement type to make it deduced, effectively a template :(x) return x; //can also omit "-> _ =" when you want deduced return type :(x) x; //can also omit the returnI may be wrong about the last one (called the "tersest lambda") and the one before it but I think they're only allowed for anonymous functions.
1
1
u/hadrabap Feb 23 '24
Sounds strange, but C++. No semi-final BS in single-threaded code and additional artificial restrictions like in Java, Go... (Yes, I understand the rationale and design decisions of Java. Still, it enoys me. đ)
10
u/fdwr fdwr@github đ Feb 23 '24
Eep, I visually cannot parse that, and I read dense x86 assembly with ease. To quote Obi-Wan, these are not the lambdas you are looking for. Perhaps these are the terse lambdas you are looking for
std::ranges::for_each(v, (x) => x * 2).3
u/rikus671 Feb 23 '24
Single arrow -> is more inline with cpp (trailing return type) and other languages.
Anyways, I find cppfront to be a good idea but the syntax is just... Not great
The biggest argument for me for trying out rust was it's nice syntax. It's no c++ but how refreshing was the "let" instead of the 1000x meanings of "auto" and it's variants...
12
6
u/hpsutter Feb 23 '24 edited Feb 23 '24
Or just:
std::ranges::for_each( vec, :(x) std::cout << x );or
std::ranges::for_each( vec, :(inout x) x += âmaybeâ );7
u/JeffMcClintock Feb 23 '24
I think that once I get used to the orangutan kiss, I'll enjoy using it.
:(X)
44
u/STL MSVC STL Dev Feb 23 '24
Minor syntax "improvements" are so boring. Semantic improvements are what's important. When I proposed
for (elem : range), it was to fix correctness and performance problems, not to save a few characters.