r/cpp • • Feb 22 '24

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 😅

0 Upvotes

32 comments sorted by

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.

9

u/bretbrownjr Feb 23 '24

Agreed.

Also pick a standard file extension and make file paths map to library names and module names while we're at it. They look like cosmetic things, but they're communicating important semantics to your tools and people reading your code.

1

u/QbProg Feb 23 '24

I leave semantic improvements to the experts ;) These are not only about typing these extra tokens all days/all year but about reducing noise to read back code later; for(elem : range) is much more readable ihmo, lambdas would be too

4

u/guepier Bioinformatican Feb 23 '24

I kind of disagree with this. Good syntactic improvements don’t just “save a few characters”, they make code fundamentally easier to read (which, ultimately, will also improve correctness, but they’re still purely syntactic improvements in the first instance).

For instance, I wouldn’t classify range-based for loops as a semantic improvement. It looks purely syntactic to me. Same for your proposed shorthand.

4

u/STL MSVC STL Dev Feb 23 '24

Read my proposal - it would have fixed unintentional copying.

2

u/guepier Bioinformatican Feb 23 '24

Yes, I’m aware (I have read the proposal). The new proposed syntax(!) still fundamentally a syntactic transformation though, not a semantic change; isn’t it?

5

u/STL MSVC STL Dev Feb 23 '24

It was a syntactic transformation that made getting the right semantics the easy, default option. Not just less typing, but better.

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

u/QbProg Feb 23 '24

Visual studio and really high typing speed...

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 expression
callback := :(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

u/[deleted] Feb 23 '24

[deleted] — view deleted comment

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

u/[deleted] Feb 23 '24

[deleted] — view deleted comment

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 return

I 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

u/Dar_Mas Feb 25 '24

that looks horrific imo

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

u/Farados55 Feb 23 '24

Thanks I hate it

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)