logoalt Hacker News

SuperV1234 • today at 1:56 PM • 3 replies • view on HN

You can write `foo({.x = 1, .y = "xyz"});` in C++20.

https://gcc.godbolt.org/z/srz4P6nzM


Replies

stephencanon • today at 2:07 PM

As is usual for C++, the committee made arbitrary changes from what C had already laid out that made the feature worse. In particular, C++ requires that the fields appear in order, which means you cannot use it to do what the linked page shows.

This isn't usually a huge deal, because C++ gives you other ways to have overloaded functions and default parameters, but it is sometimes mildly annoying.

➕ show 1 reply
cocoto • today at 2:07 PM

Sure but now you have to make sure the order of the members is correct.

xKingfisher • today at 2:00 PM

And this form is much safer if you're using span/string_view members in your struct.