C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Implicitly accepting leading default function/template argument values

From: Barry Revzin <barry.revzin_at_[hidden]>
Date: Sun, 6 Mar 2022 14:13:18 -0600
On Sun, Mar 6, 2022 at 9:54 AM Arthur O'Dwyer via Std-Proposals <
std-proposals_at_[hidden]> wrote:

> On Sun, Mar 6, 2022 at 10:26 AM David Jones via Std-Proposals <
> std-proposals_at_[hidden]> wrote:
>
>> Consider the function
>>
>> void foo(int i = 1, int j = 2, int k = 3);
>>
>> *Trailing *default argument values can be accepted implicitly by simply skipping
>> them in the function call:
>>
>> foo(); //Accept default values for i, j, and k
>> foo(1); //Accept default values for j and k, supply value for i
>> foo(1,2); //Accept default value for k, supply values for i and j
>>
>> But to my knowledge, there is no method for implicitly accepting
>> *leading *default argument values - they must be copied out explicitly:
>>
>> foo(1,2,5); //Accept default values for i and j by copying them out
>> explicitly, supply value for k
>>
>> I suggest that this could be done implicitly thus:
>>
>> foo(,,2); //Accept default values for i and j, supply value for k
>> foo(,1); //Accept default values for i and k, supply value for j
>>
>
> That would close the door on the vastly vastly more useful Python-style
> syntax
> x = f(
> 1,
> 2,
> )
> which in C++ today means "syntax error," but in Python and other modern
> languages means the same as "x = f(1, 2)".
> Under your proposal, it would have to mean the same as "x = f(1, 2, /*some
> defaulted thing*/)".
>

I'm not sure what about this is useful. Maybe in the general sense of
always allowing an extra trailing comma, which works in some C++ contexts
but not others, which makes code gen annoying sometimes?

But there is a different vastly more useful Python-style syntax here that
solves the original problem, namely:

f(k=2); // Accept default values for i and j, supply value for k
f(j=1); // Accept default values for i and k, supply value for j

There have been several named argument proposals in the past, including
Andrew Tomazos' latest entry (which I think is promising). I'd rather we go
in that direction. Named arguments solve a lot more problems than simply
the ability to pass default values to intermediate ones, and named
arguments solve this particular problem quite a bit better than the
suggested syntax here too.

Barry

Received on 2022-03-06 20:13:34