C++ Logo

std-proposals

Advanced search

Re: Video Intro to WG21/D2288 C++ Designated Arguments

From: Ville Voutilainen <ville.voutilainen_at_[hidden]>
Date: Mon, 22 Nov 2021 21:37:47 +0200
On Mon, 22 Nov 2021 at 21:22, Hani Deek via Std-Proposals
<std-proposals_at_[hidden]> wrote:
>
> I have not studied this issue carefully, but I think that one simple way to implement your proposal without introducing new things into the language is to allow the following syntax
>
> void f(int x = 0, struct { int y = 43; int z = 23; } v = {});
>
> As a shorthand for the following syntax
>
> struct S { int y = 43; int z = 23; };
> void f(int x = 0, S v = {});
>
> That is, allow a struct/class to be declared and defined inside the function declaration. The struct/class may be named or not.
>
> Then we can call the function in a regular way
>
> f(0, { .y{43}, .z{23} });
>
> Of course, the syntax that you propose is more brief. However, the brevity comes at the expense of complicating the language by introducing completely new things and completely new syntax.

Right. And for "but I just want to pass two ints", it doesn't actually
seem like that much of a burden to write
void f(struct {int a = 42; int b = 666;} oink = {});

and just use oink.a and oink.b in the function body. The declaration
opts in, there's already a calling syntax that allows
you to set just the fields you want to set, and yeah sure there's an
ordering thing there, but it's not surprising because
it's already elsewhere and can be diagnosed. A boatload of questions
that arise when looking at a new special syntax
don't actually arise here, because "it works like structs elsewhere work".

In other words, there's a lot to like about this propsal. It doesn't
sing and dance, but it certainly seems like a much easier
fit.

There's not even a need for that ={} at the end, it's just there to
allow calling such a function with zero arguments, not
even {}.

Received on 2021-11-22 13:38:00