News for hackers. Stuff that matters.
Re:Chaining, or why you should stop returning void (2012) by jimbob45 on Sunday November 07, 2021 @11:07AM (#29140575)
I know C is an extremely deep language but that doesn’t look like C99 to me. Since when can you set struct member variables piecewise in a…function definition?

Is this maybe a custom compiler? Or is this actually C++?

Re:Chaining, or why you should stop returning void (2012) by MayeulC on Sunday November 07, 2021 @12:07PM (#29141230)
As other commenters have pointed out, those are designated initializers[1], one of my favorite C99 feature.

I's also been picked up recently for inclusion into C++20[2].

However, it seems that C++ adds a specific limitation:

> all designators used in the expression must appear in the same order as the data members of T

> out-of-order designated initialization, nested designated initialization, mixing of designated initializers and regular initializers, and designated initialization of arrays are all supported in the C programming language, but are not allowed in C++

Which limits the usefulness, as I've used this syntax to let myself reorder struct fields (and add some) without breaking the API (while obviously breaking the ABI). It's a great tool to use while your API is still in flux.

At least an error will be produced, which is much better than just providing values and hoping that the API/ABI remains stable.

[1]: https://en.cppreference.com/w/c/language/struct_initializati...

[2]: https://en.cppreference.com/w/cpp/language/aggregate_initial...

Re:Chaining, or why you should stop returning void (2012) by kevin_thibedeau on Sunday November 07, 2021 @11:26AM (#29140795)
It's a C99 compound literal with designated initializer. C++ doesn't support it.
Re:Chaining, or why you should stop returning void (2012) by rackjack on Sunday November 07, 2021 @11:23AM (#29140758)
It is C99, which surprised me too.

https://stackoverflow.com/a/7487949/11736447

Re:Chaining, or why you should stop returning void (2012) by chisquared on Sunday November 07, 2021 @11:23AM (#29140760)
I encountered code like this in a C99 project recently. I still don't understand it, but I believe it's supposed to be possible.
Re:Chaining, or why you should stop returning void (2012) by nwellnhof on Sunday November 07, 2021 @11:22AM (#29140753)
What you're seeing is a compound literal with designated initializers. Both features were introduced in C99.