top | item 43008346

(no title)

tjalfi | 1 year ago

C++20 added designated initializers, so they're also an option.

    my_func({.arg1 = val1, .arg2 = val2});

discuss

order

anitil|1 year ago

Oh I thought that was a C99 addition? It's been a while since I've used them.

Edit: struggling to find a source, though this GCC doc suggests it's C99 (but maybe that's only GCC?) - https://gcc.gnu.org/onlinedocs/gcc/Designated-Inits.html

AlotOfReading|1 year ago

It was C99. It took C++ 21 years to copy it.

Cyph0n|1 year ago

Well, C is not the same as C++. It even says in that doc that “this extension is not implemented in C++”.

pragma_x|1 year ago

Even though you need to declare a struct for this (or other) functions to receive these fields, I feel like this is the cleanest approach. What I'm not sure about is if reference, value, or pointer is the best way to let the compiler optimize this.