C++ Logo

std-proposals

Advanced search

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

From: Magnus Fromreide <magfr_at_[hidden]>
Date: Thu, 15 Dec 2022 02:39:27 +0100
On Tue, Dec 13, 2022 at 04:13:33PM -0600, Kyle Knoepfel via Std-Proposals wrote:
> 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});

How would this handle

double distance(coordinate a, coordinate b);

What does

distance(.x=1, .y=2);

mean? Is it even valid?

/MF

Received on 2022-12-15 01:39:31