C++ Logo

std-proposals

Advanced search

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

From: Jason McKesson <jmckesson_at_[hidden]>
Date: Sun, 6 Mar 2022 11:15:07 -0500
On Sun, Mar 6, 2022 at 10: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*/)".

Would it? I think it'd be pretty easy to have leading or intermixed
commas mean "some defaulted thing" but to have a trailing comma mean
"some defaulted thing or nothing". Last commas have to be a
special-case rule to get Python syntax anyway, so specifying that
special-case slightly differently shouldn't be a problem.

Received on 2022-03-06 16:15:09