C++ Logo

std-proposals

Advanced search

[std-proposals] Designated initializers without braces (aka keyword arguments)

From: Kyle Knoepfel <kyleknoepfel_at_[hidden]>
Date: Tue, 13 Dec 2022 16:13:33 -0600
Hi all,

C++ language support for keywords arguments (i.e. the ability to explicitly specify a function parameter name at a function's call site) has been raised several times--e.g. https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2018/p0671r2.html <https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2018/p0671r2.html>. And for many reasons, it has not gained much traction. It has also been mentioned in various places that a workaround possible since C++20 is to use designated initializers:

struct coordinate { int x; int y; };
void value_at(coordinate c);

auto value = value_at({.x=1, .y=2}); // initialize temporary coordinate

which isn't too bad.

The question is: how much of a stretch would it be to allow the elision of braces when designated initializers are specified in a function call? In other words:

auto value = value_at(.x=1, .y=2); // initialize temporary coordinate with elided braces, equivalent to ...
auto value = value_at({.x=1, .y=2});

This grants the usability that natively supported keyword arguments would provide for function users, while placing the burden of providing such usability on function authors (without introducing new ideas wrt how function arguments are handled).

Granted, this would affect every parser in the world and I, personally, am not sure I'm excited enough about this to pursue it with much effort. That said, I'm curious to your thoughts about this.

Thanks,
Kyle

Received on 2022-12-13 22:13:44