C's designated initializers quietly deliver named arguments
C's designated initializers give us named arguments
C's full-power designated initializers, which C++20 didn't adopt, can be combined with variadic macros to give C functions named, defaultable arguments with ordinary call syntax. Designators may appear in any order, and repeated ones let callers override defaults. Adding a new parameter then stops being an API break, though it remains an ABI break. The author calls the pattern heavyweight and cute, yet finds real uses in projects like EpicGames/raddebugger.
That second bullet means we're relying on something that would be bad style if it weren't intentional.
- quietbritishjim
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.
- s3graham
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.
- binaryturtle
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.
- cryptonector
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.
- nixpulvis
Treating named arguments as structs is my proposal for Rust as well: https://nixpulvis.com/ramblings/2026-10-19-anonymous-struct-...