C++ Logo

std-proposals

Advanced search

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

From: David Jones <davidjones1105_at_[hidden]>
Date: Sun, 6 Mar 2022 21:17:04 +0000
I prefer your suggested syntax to mine.

I am interested as to how "overriding defaults in virtual functions" could be implemented. Would this not require delaying default argument insertion from compile-time to runtime?

________________________________
From: Gergely Nagy <gergely.nagy.alt_at_[hidden]>
Sent: 06 March 2022 20:25
To: std-proposals_at_[hidden] <std-proposals_at_[hidden]>
Cc: Barry Revzin <barry.revzin_at_[hidden]>; David Jones <davidjones1105_at_[hidden]>
Subject: Re: [std-proposals] Implicitly accepting leading default function/template argument values

Hey,

I actually planned to write a detailed proposal on default values, bc I think they are kinda restrictive currently.

What I suggest for this particular use case to simply use the default keyword, because it is not ambiguous in this context.

   f(1,default,3)

would be the proposed syntax. I have other ideas, for example inheriting (in class inheritance) and overriding defaults in virtual functions. (This is not as hard as one might think.)

Thanks,
   Gergely

Barry Revzin via Std-Proposals <std-proposals_at_[hidden]<mailto:std-proposals_at_[hidden]>> ezt írta (időpont: 2022. márc. 6., V, 21:13):


On Sun, Mar 6, 2022 at 9:54 AM Arthur O'Dwyer via Std-Proposals <std-proposals_at_[hidden]<mailto:std-proposals_at_[hidden]>> wrote:
On Sun, Mar 6, 2022 at 10:26 AM David Jones via Std-Proposals <std-proposals_at_[hidden]<mailto: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
--
Std-Proposals mailing list
Std-Proposals_at_[hidden]<mailto:Std-Proposals_at_[hidden]>
https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals

Received on 2022-03-06 21:17:06