C++ Logo

std-proposals

Advanced search

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

From: Barry Revzin <barry.revzin_at_[hidden]>
Date: Mon, 22 Nov 2021 21:42:42 -0600
On Mon, Nov 22, 2021 at 9:34 PM Jason McKesson via Std-Proposals <
std-proposals_at_[hidden]> wrote:

> On Mon, Nov 22, 2021 at 2:22 PM 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
>
> Your suggestion misses one of the most important parts of the
> proposal: handling variadic templates and forwarding. Specifically,
> the ability to forward designators through a function. Your
> alternative can't do that.
>
> Indeed, the only way to call such a function through forwarding is to
> explicitly construct a parameter of the proper type, which requires
> being able to *name* the type, which you can't do if you declared the
> parameter in the function declaration.
>
> So adopting this syntax basically makes forwarding impossible.
> --
> Std-Proposals mailing list
> Std-Proposals_at_[hidden]
> https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals


So the idea here just reduces this:

struct Args { int x = 1, y = 2; };
void f(Args args);

to this:

void f(struct { int x = 1, y = 2 } args );

Even though the former requires me to provide a name for a type whose name
I never need at all, I'm not even sure that the latter is better? It's
shorter, but it doesn't buy me anything - it's just harder to understand? I
would still continue to write the former.

Jason brings up variadic templates and forwarding, but we don't even have
to go that far. While using designated initializers with a named struct
lets us kind of mock out named arguments, it doesn't let us do any kind of
template at all. Like, this doesn't work:

template <typename T> struct Args { T x; int y = 0; };
template <typename T> void f(Args<T> args);
f({.x=5}); // nope

I'm not even sure what the syntax would be for defining Args<T> in-line,
but whatever it is, it's dramatically worse than being able to declare:

template <typename T> void f(T x, int y = 0);

And would require a whole new language feature you need just, just to
support deducing a named parameter?

Barry

Received on 2021-11-22 21:42:56