Date: Fri, 27 Nov 2026 09:20:10 +0100
On 26/11/2026 22:32, Alejandro Colomar via Std-Proposals wrote:
> Hi Bo,
>
>> Date: 2026-11-26 21:29:02+0100
>> From: Bo Persson via Std-Proposals <std-proposals_at_[hidden]>
>>
>> On 2026-11-26 at 15:50, Alejandro Colomar via Std-Proposals wrote:
>>> Hi Bo,
>>>
>>>> Date: 2026-11-26 15:28:21+0100
>>>> From: Bo Persson via Std-Proposals <std-proposals_at_[hidden]>
>>>>
>>>> On 2026-11-26 at 12:47, Alejandro Colomar via Std-Proposals wrote:
>>>>>
>>>>> The proposal for C++ would then be so that one can write code like this:
>>>>>
>>>>> int foo(int a = 42, int b);
>>>>>
>>>>> And call it as
>>>>>
>>>>> foo(, 7); // Evaluates as foo(42,7)
>>>>>
>>>>> This would replace some cases which are currently handled with
>>>>> overloads, which are a much heavier machinery (and also more difficult
>>>>> to diagnose).
>>>>>
>>>>>
>>>> How is an overload "much heavier"?
>>>>
>>>> To me
>>>>
>>>> int foo(int b) { return foo(42, b); }
>>>
>>> It requires more source code. Consider for example, if you have 2 or 3
>>> parameters with default arguments, you'll need to write 2^2 or 2^3
>>> overloads, which grows exponentially.
>>>
>> But you don't have that. :-)
>
> 2 functions where we could do with 1 is already an improvement, IMO.
> And having better diagnostics is also an improvement.
> And not having to guess which overload is being used is also nice.
>
>>
>> If you have 3 default parameters, all in the wrong order, something is
>> seriously wrong. I would refactor that code to make it better.
>
> There are some cases where you need to have the arguments in a specific
> order for historic reasons.
>
>
> Have a lovely night!
> Alex
>
>
Functions with lots of parameters are always a risk from the readability
and correctness perspective - it's too easy to get them wrong, or
misunderstand them. Default parameters as we have them today, often
makes this worse. Allowing different parameters in the middle to be
defaulted strikes me as a recipe for disaster.
If you /really/ need to give lots of arguments to a function, several of
which are often default values, there are multiple ways to make it safer
and clearer. You can have wrapper functions - as overloads, or with
slightly different names - for different combinations of actual
arguments to use. You can use strong type wrappers around the
parameters to give parameter names - that way they cannot be mixed up.
But the method that seems most appropriate to your situation is passing
a struct of the arguments, and using designated initialisers. So
instead of writing :
int foo(int x = 123, int y = 42, int z = 7);
int a = foo(124, , 8); // Clear as mud
you write :
struct foo_ { int x = 123; int y = 42; int z = 7; };
int foo(foo_ args);
int a = foo(foo_{ .x = 124, .z = 8 }); // Obvious
or :
struct foo {
int x = 123; int y = 42; int z = 7;
int operator() { ... }
};
int a = foo{ .x = 124, .z = 8 }(); // Less verbose
For more complicated situations, give "foo_" constructors.
(The simple struct version works fine in C too. Inline wrapper
functions are also fine in C, albeit without the overloads.)
> Hi Bo,
>
>> Date: 2026-11-26 21:29:02+0100
>> From: Bo Persson via Std-Proposals <std-proposals_at_[hidden]>
>>
>> On 2026-11-26 at 15:50, Alejandro Colomar via Std-Proposals wrote:
>>> Hi Bo,
>>>
>>>> Date: 2026-11-26 15:28:21+0100
>>>> From: Bo Persson via Std-Proposals <std-proposals_at_[hidden]>
>>>>
>>>> On 2026-11-26 at 12:47, Alejandro Colomar via Std-Proposals wrote:
>>>>>
>>>>> The proposal for C++ would then be so that one can write code like this:
>>>>>
>>>>> int foo(int a = 42, int b);
>>>>>
>>>>> And call it as
>>>>>
>>>>> foo(, 7); // Evaluates as foo(42,7)
>>>>>
>>>>> This would replace some cases which are currently handled with
>>>>> overloads, which are a much heavier machinery (and also more difficult
>>>>> to diagnose).
>>>>>
>>>>>
>>>> How is an overload "much heavier"?
>>>>
>>>> To me
>>>>
>>>> int foo(int b) { return foo(42, b); }
>>>
>>> It requires more source code. Consider for example, if you have 2 or 3
>>> parameters with default arguments, you'll need to write 2^2 or 2^3
>>> overloads, which grows exponentially.
>>>
>> But you don't have that. :-)
>
> 2 functions where we could do with 1 is already an improvement, IMO.
> And having better diagnostics is also an improvement.
> And not having to guess which overload is being used is also nice.
>
>>
>> If you have 3 default parameters, all in the wrong order, something is
>> seriously wrong. I would refactor that code to make it better.
>
> There are some cases where you need to have the arguments in a specific
> order for historic reasons.
>
>
> Have a lovely night!
> Alex
>
>
Functions with lots of parameters are always a risk from the readability
and correctness perspective - it's too easy to get them wrong, or
misunderstand them. Default parameters as we have them today, often
makes this worse. Allowing different parameters in the middle to be
defaulted strikes me as a recipe for disaster.
If you /really/ need to give lots of arguments to a function, several of
which are often default values, there are multiple ways to make it safer
and clearer. You can have wrapper functions - as overloads, or with
slightly different names - for different combinations of actual
arguments to use. You can use strong type wrappers around the
parameters to give parameter names - that way they cannot be mixed up.
But the method that seems most appropriate to your situation is passing
a struct of the arguments, and using designated initialisers. So
instead of writing :
int foo(int x = 123, int y = 42, int z = 7);
int a = foo(124, , 8); // Clear as mud
you write :
struct foo_ { int x = 123; int y = 42; int z = 7; };
int foo(foo_ args);
int a = foo(foo_{ .x = 124, .z = 8 }); // Obvious
or :
struct foo {
int x = 123; int y = 42; int z = 7;
int operator() { ... }
};
int a = foo{ .x = 124, .z = 8 }(); // Less verbose
For more complicated situations, give "foo_" constructors.
(The simple struct version works fine in C too. Inline wrapper
functions are also fine in C, albeit without the overloads.)
Received on 2026-11-27 08:20:15
