C++ Logo

std-proposals

Advanced search

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

From: Antoine Viallon <antoine_at_[hidden]>
Date: Thu, 25 Nov 2021 20:14:30 +0000
I personally very much dislike using a struct for passing named arguments.
 * First of all, it is unnecessary boilerplate.
 * It is not possible to reorder named args with a struct, while it could be implemented with this proposal
 * Having an explicit syntax for named arguments (like what already exists in many other languages) allows for additional features, like optional named args. This would mean very clean code, easy to understand function prototypes, with no cost at runtime.
 * Using a struct for named args means that, every time the function is called, an entire struct is allocated (on the stack), even if only one or two "named" parameters are actually used.

Antoine Viallon

23 novembre 2021 04:42 "Barry Revzin via Std-Proposals" <std-proposals_at_[hidden] (mailto:std-proposals_at_[hidden]?to=%22Barry%20Revzin%20via%20Std-Proposals%22%20<std-proposals_at_[hidden]>)> a écrit:
On Mon, Nov 22, 2021 at 9:34 PM Jason McKesson via Std-Proposals <std-proposals_at_[hidden] (mailto:std-proposals_at_[hidden])> wrote: On Mon, Nov 22, 2021 at 2:22 PM Hani Deek via Std-Proposals
<std-proposals_at_[hidden] (mailto: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] (mailto:Std-Proposals_at_[hidden])
https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals (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-25 14:14:39