C's designated initializers give us named arguments quuxplusone.github.io · 18 p...
HN Best 200 @best
C's designated initializers give us named arguments quuxplusone.github.io · 18 points · 11 comments · HN thread
TL;DR Quuxplusone's blog post shows how C99 designated initializers, combined with a compound literal and a macro, emulate named, defaultable function arguments. The idiom, taken from Ben Klemens' book 21st Century C, lets new fields be added to the Args struct without breaking call sites, though it is an ABI break. The post notes it is heavyweight and rarely worth using, and cites real uses in GitHub projects such as raddebugger.
- C designated initializers allow fields in any order and let the same designator repeat, with the last value winning; C++20 adopted neither.
- A macro wraps a compound literal of a struct with default values, and the caller overrides only the fields they need, e.g. ideal_volume(.kelvin = 373.15).
- Adding a new named field to the Args struct does not break existing call sites at the source level, but it changes the ABI, so callers must be recompiled.
- Clang warns by default with -Winitializer-overrides; GCC only warns with -Wextra via -Woverride-init.
- The author stresses this is cute but heavyweight, and a plain function with positional parameters is usually enough.
- Real uses found include gab462/hof-flecs-raylib and EpicGames/raddebugger's ui_anim macro.
Discussion Commenters mostly compare the C technique with C++ alternatives, with the consensus that C++20 designated initializers are weaker because fields must appear in declaration order. Several point to simple workarounds such as filling a struct in separate statements, or passing a braced initializer. The thread also drifts into older patterns: a tag-list API on the Amiga and switch-based generator tricks like Duff's Device and protothreads. The mood is curious and appreciative, with a few practical caveats rather than arguments.
- C++20 designated initializers are inferior to C's (many): Commenters note that C++ requires fields in declaration order, which blocks the out-of-order and repeated-designator tricks. stephencanon calls the committee's changes arbitrary and says they make the feature much worse.
- Use a braced designated initializer in C++20 (some): SuperV1234 shows that foo({.x = 1, .y = "xyz"}) works in C++20 for many cases. xKingfisher adds that this is safer when structs hold span or string_view members.
- Separate-statement struct setup works in C++ (some): quietbritishjim suggests declaring a params struct with defaults and assigning fields one by one, though it costs extra moves when passing by value.
- Field order requirement is a maintenance hazard (a few): cocoto points out that with ordered designators you must keep member order correct when the struct changes.
- Old systems had this idea already (a few): binaryturtle recalls the Amiga tag-list concept, which allowed API extension without ABI breaks and gave named arguments.
- Switch-based generators are a related trick (a few): robmccoll and mtklein discuss emulating yield with switch statements in C, compared with Duff's Device, and note that static state trades flexibility for encapsulation.
Links from the comments