C++ Logo

std-proposals

Advanced search

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

From: Hani Deek <hani_deek_at_[hidden]>
Date: Mon, 7 Mar 2022 18:06:45 +0000
I love your proposal, because it can save tons of code. It allows us to use the brief syntax of default arguments instead of having to write multiple function overloads to supply missing argument values that are not trailing arguments.

However, whenever you suggest something that has the effect of drastically reducing the amount of required code, people will object on the grounds that your suggestion opens the door for misinterpreting typos and unintended mistakes.

Naturally, the less you write, the less clear your intention becomes.

C++ generally gives favor to typo/mistake avoidance over brevity, although occasionally there were changes that violated this principle, such as the 'Down with typename' proposal of C++20. I love the fact that that proposal was adopted because it has allowed us to avoid many unnecessary uses of the 'typename' keyword, but that proposal went against the usual direction of C++.

Perhaps Gergely's idea of using the default keyword instead of providing zero argument can overcome the objection that the proposal will lead to misinterpretation of typos.

In C++, function calls and aggregate initialization have different syntax rules even though it is conceptually possible to use the same syntax for both. A good thing about your proposal is that it does not introduce additional divergence between the function call syntax and aggregate initialization syntax, since your proposal can used equally for both.


________________________________
From: Std-Proposals <std-proposals-bounces_at_[hidden]> on behalf of David Jones via Std-Proposals <std-proposals_at_[hidden]>
Sent: Sunday, March 6, 2022 9:26 AM
To: std-proposals_at_[hidden] <std-proposals_at_[hidden]>
Cc: David Jones <davidjones1105_at_[hidden]>
Subject: [std-proposals] Implicitly accepting leading default function/template argument values

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

This could also apply to default template arguments. For example, to specify a custom allocator for std::set the comparison type must currently also be explicitly specified :

std::set<int, std::less<int>, Myalloc>; //OK, but std::less<int> is superfluous.
std::set<int,, Myalloc>; //Currently an error

Thanks, David

Received on 2022-03-07 18:06:48