C++'s version of this is more verbose but still sometimes worth it: make the caller instantiate the struct and initialise it as separate statements:
FooParams fooParams; // Gives sensible defaults to all members
fooParams.x = 1;
fooParams.y = "xyz";
foo(fooParams);
It doesn't work so well if you want to move construct things because you need to move at least twice: once when moving the value into the member, then you need to move the whole struct to the function argument `foo(std::move(fooParams))`. If you want it to copy something but avoid unnecessary copying then it's even more annoying because you need to move to the function argument still which looks odd when your goal is to copy overall.
I always thought Amiga's "tag list" concept was really nice. You easily could extend the API w/o breaking the ABI, which was essential for its shared library concept. And it essentially gave you named arguments too.
Yes indeed. I've often wondered where it came from - did they invent it, or was it inspired by something on another platform?
(For the uninitiated, taglists are freeform lists of TAG / value pairs, which you would typically supply through varargs functions - something like OpenScreenTags(&newscreenstruct,SA_WIDTH,320,SA_HEIGHT,256,SA_DISPLAYID,myscreenmode,TAG_DONE); )
Yes, that was neat at the time: an array of key/value structs for extensibility. The "keys" were a #define somewhere in the header files. The original Amiga APIs (before OS 2.0) did not have tag lists and it was painful to work with.
I started learning C on an Amiga with OS 1.3, then moved to a 2.0 machine. Fun times.
A somewhat similar extension concept (in the sense of: allow to extend the API without breaking ABI) still exists in some C APIs, for instance in Vulkan each struct starts with an `sType` tag and a `pNext` pointer, this allows to build 'extension struct chains':
I proposed something like this ~20 years ago in the context of GSS-API, where there are several functions with lots of arguments but where most arguments have reasonable defaults, so one could use named arguments for all the values that one must give and not mention the others that get default values. But I wasn't serious about it.
I always considered this kind of a hack, needing to wrap the actual function call with a function-like macro. Moreover, you’re now using a different calling convention with passing a struct instead of basic types which can be less efficient. I wish C would add actual named arguments like I added in my DrC project: https://github.com/drpriver/DrC/blob/master/EXTENSIONS.md#na...
The idiom I usually use is to define a DEFAULT_FOO_PARAMS like
struct foo { int Z; int A; double E, double v };
#define DEFAULT_FOO_PARAMS .Z = 1, v = 3e8
foo_fn ( &(struct foo){ DEFAULT_FOO_PARAMS, .Z =2, .A = 4, .E = 1 });
But I do sort of wish there was a language-supported way to set defaults for a designated initializer in the struct definition (like C++ default values) to avoid having to write DEFAULT_FOO_PARAMS there.
It's fun watching people rediscover this every so often. If you like this sort of thing, go check out creating generator functions using a switch embedded throughout your function. You can wrap it in macros to end up with a nice yield keyword emulation.
Is this essentially the mechanism behind Duff's Device (switch cases being better thought of as line-oriented jumps instead of structured blocks) but generalised?
The macro is cute, but passing structures by value like that is going to cause real measurable overhead where the functions don't get inlined (the author sort of admits this I guess by calling it "heavyweight").
Tbh I would skip all the preprocessor magic proposed in the article and just go for plain C99 'option bag struct' arguments, the called function only needs to be aware that "zero means default".
I've been using this style for almost 10 years now in the sokol headers, and it still feels like the right thing to do and actually more ergonomic than most other languages for initializing complex structs, e.g. see here for creating a 3D render pipeline object with dozens of creation options, but most of them keeping their default values:
...this code snippet also makes use of C99 features that are not available in C++20 though (or most other languages), and it's really remarkable how well designed the C99 designated init feature as a whole is (even though it was an afterthought) and how badly its individual subfeatures were understood by most later languages (basically, only Odin got it "mostly right": https://github.com/floooh/sokol-odin/blob/7d47a31cad83a26ba1...)
I had never seen array designated initializer combined with the struct variant. Great foresight of whomever thought to make these features so flexible and composable:
I do wonder how many times setting the same member twice is a bug rather than a feature. I can imagine quite a bit of copy/paste leading to errors like this.
I also wonder what happens when you forget to give a mandatory parameter which has no default... Yes, that's why this has been possible for a long time but barely anyone uses it : it's a footgun.
I both love & hate this. I would genuinely consider using it in my own projects, and also I would be very pissed off if I encountered it in someone else's project. Brilliant.
C++'s version of this is more verbose but still sometimes worth it: make the caller instantiate the struct and initialise it as separate statements:
It doesn't work so well if you want to move construct things because you need to move at least twice: once when moving the value into the member, then you need to move the whole struct to the function argument `foo(std::move(fooParams))`. If you want it to copy something but avoid unnecessary copying then it's even more annoying because you need to move to the function argument still which looks odd when your goal is to copy overall.You can write `foo({.x = 1, .y = "xyz"});` in C++20.
https://gcc.godbolt.org/z/srz4P6nzM
Aggregates get RVO into members too, so say .y is a std::string then "xyz" is going to construct in place.
I would prefer the java style FooInvocation().x(1).y("xyz").run() over that
I always thought Amiga's "tag list" concept was really nice. You easily could extend the API w/o breaking the ABI, which was essential for its shared library concept. And it essentially gave you named arguments too.
Yes indeed. I've often wondered where it came from - did they invent it, or was it inspired by something on another platform?
(For the uninitiated, taglists are freeform lists of TAG / value pairs, which you would typically supply through varargs functions - something like OpenScreenTags(&newscreenstruct,SA_WIDTH,320,SA_HEIGHT,256,SA_DISPLAYID,myscreenmode,TAG_DONE); )
Yes, that was neat at the time: an array of key/value structs for extensibility. The "keys" were a #define somewhere in the header files. The original Amiga APIs (before OS 2.0) did not have tag lists and it was painful to work with.
I started learning C on an Amiga with OS 1.3, then moved to a 2.0 machine. Fun times.
A somewhat similar extension concept (in the sense of: allow to extend the API without breaking ABI) still exists in some C APIs, for instance in Vulkan each struct starts with an `sType` tag and a `pNext` pointer, this allows to build 'extension struct chains':
https://docs.vulkan.org/refpages/latest/refpages/source/VkDe...
I proposed something like this ~20 years ago in the context of GSS-API, where there are several functions with lots of arguments but where most arguments have reasonable defaults, so one could use named arguments for all the values that one must give and not mention the others that get default values. But I wasn't serious about it.
Love it too! https://mstdn.social/@sgraham/112486947826240650
As I mentioned there, I think it's even cooler when you collapse the "constructor" macro, and tag to be the same name.
I always considered this kind of a hack, needing to wrap the actual function call with a function-like macro. Moreover, you’re now using a different calling convention with passing a struct instead of basic types which can be less efficient. I wish C would add actual named arguments like I added in my DrC project: https://github.com/drpriver/DrC/blob/master/EXTENSIONS.md#na...
If you don't need defaults, you don't need the macro.
You still get the advantage of not scrambling the order of your same-typed arguments.
It's more verbose and a different calling convention, but it might be worth it.
The idiom I usually use is to define a DEFAULT_FOO_PARAMS like
But I do sort of wish there was a language-supported way to set defaults for a designated initializer in the struct definition (like C++ default values) to avoid having to write DEFAULT_FOO_PARAMS there.It's fun watching people rediscover this every so often. If you like this sort of thing, go check out creating generator functions using a switch embedded throughout your function. You can wrap it in macros to end up with a nice yield keyword emulation.
And go take a look at:
- the old Icon to-C compiler
- PuTTY's co-routines (and Simon Tatham's essay on C co-routines[0])
[0] https://www.chiark.greenend.org.uk/~sgtatham/coroutines.html
Is this essentially the mechanism behind Duff's Device (switch cases being better thought of as line-oriented jumps instead of structured blocks) but generalised?
The macro is cute, but passing structures by value like that is going to cause real measurable overhead where the functions don't get inlined (the author sort of admits this I guess by calling it "heavyweight").
Tbh I would skip all the preprocessor magic proposed in the article and just go for plain C99 'option bag struct' arguments, the called function only needs to be aware that "zero means default".
I've been using this style for almost 10 years now in the sokol headers, and it still feels like the right thing to do and actually more ergonomic than most other languages for initializing complex structs, e.g. see here for creating a 3D render pipeline object with dozens of creation options, but most of them keeping their default values:
(or see here: https://github.com/floooh/sokol-samples/blob/11bbe1a6865e93c...)...this code snippet also makes use of C99 features that are not available in C++20 though (or most other languages), and it's really remarkable how well designed the C99 designated init feature as a whole is (even though it was an afterthought) and how badly its individual subfeatures were understood by most later languages (basically, only Odin got it "mostly right": https://github.com/floooh/sokol-odin/blob/7d47a31cad83a26ba1...)
That is beatiful.
I had never seen array designated initializer combined with the struct variant. Great foresight of whomever thought to make these features so flexible and composable:
Treating named arguments as structs is my proposal for Rust as well: https://nixpulvis.com/ramblings/2026-10-19-anonymous-struct-...
I do wonder how many times setting the same member twice is a bug rather than a feature. I can imagine quite a bit of copy/paste leading to errors like this.
I also wonder what happens when you forget to give a mandatory parameter which has no default... Yes, that's why this has been possible for a long time but barely anyone uses it : it's a footgun.
I both love & hate this. I would genuinely consider using it in my own projects, and also I would be very pissed off if I encountered it in someone else's project. Brilliant.