C++ Logo

std-proposals

Advanced search

Re: default input value

From: Edward Diener <eldlistmailingz_at_[hidden]>
Date: Tue, 12 Nov 2019 17:33:38 -0500
On 11/12/2019 2:09 PM, Vishal Oza via Std-Proposals wrote:
> I would like to propose a new language feature into the language.
>
> The feature is to allow writing default in function values to have
> default value feed into the function/method as a part of the language.
>
> int foo(int bar = 3, int baz = 7, int* foobar = nullptr, int* foobaz =
> nullptr) { ... }
> ...
>
> int foobazval = 78;
>
> auto fooval = foo(13, default, default, &foobazval); // = foo(13,
> 7, nullptr, &foobazval)
>
> if the function have no default value calling default would be the same
> as calling the default constructor. Errors would include assigning a
> default value to a reference type and assigning default to a type with
> no default constructor.

As I said previously I like your proposal except for:

1) Specifying 'default' for a parameter which has no default should be
illegal and a compiler error.
2) There is absolutely no reason why a reference type parameter with a
default value, which is perfectly legal but rarely specified, should be
treated any differently than any other default value.

My two objections is to keep this proposal as simple as possible without
creating totally unnecessary one-off cases.

I would also add that default parameters in C++ also occur in class and
function templates and there is no reason why the same use of the
'default' keyword could not be used in those situations also as part of
your proposal.

>
> As a side note I have been told that as a extension allowing a to
> specify function names could also solve the same problem but I do not
> want to push for this hard as I think is can break code where the
> interface definition does not match the implementation. This second
> approach would allow C++ developer to only assign to field the the need
> in as order they want like in the example bellow.
>
> auto fooval = foo( bar : 13, foobaz : &foobazval);

As I noted preciously Boost already has a named parameter library called
Boost Parameter for those who want to use named parameters, but your
basic proposal should be seen as an easy to use alternative to that with
almost no overhead and a clear syntactical ease of use.

Received on 2019-11-12 16:36:06