Date: Mon, 20 Apr 2026 15:12:07 +0200
On 20/04/2026 14:39, Alejandro Colomar via Std-Proposals wrote:
> Hi,
>
> On 2026-04-20T15:25:08+0300, sasho648 via Std-Proposals wrote:
>> Bump now I had:
>>
>> like:
>>
>> void f(int a, bool d = false);
>>
>> int f1(int a, int *p = nullptr);
>>
>> and I called:
>>
>> int *p;
>>
>> f(f1(2), p);
>>
>>
>> It compiles and runs "fine" - except the behavior is totally unintended.
>>
>> It's evil in my opinion.
>
> C++ default parameters are evil, indeed.
>
> A better design for default parameters would be leaving the parameter
> empty, which would make sure you don't misplace things.
>
> f(f1(2, p),);
>
> That extra comma makes sure you know what you're doing, by explicitly
> asking for the default value. I do default parameters in C like that,
> which can be easily implemented with macros.
>
> In general, I would recommend avoiding default parameters in C++.
You can usually use overloads to avoid default parameters :
void f(int a, bool b);
void f(int a) { f(a, false); }
int f1(int a, int * p);
int f1(int a) { return f1(a, nullptr); }
It does not help the OP's problem in this case (the cause is the
implicit conversion of pointers to bool, not the default parameters).
But it avoids another aspect of default parameters that I dislike - the
loss of symmetry between declarations (with the default value) and
definitions (which must not have the default value).
> Hi,
>
> On 2026-04-20T15:25:08+0300, sasho648 via Std-Proposals wrote:
>> Bump now I had:
>>
>> like:
>>
>> void f(int a, bool d = false);
>>
>> int f1(int a, int *p = nullptr);
>>
>> and I called:
>>
>> int *p;
>>
>> f(f1(2), p);
>>
>>
>> It compiles and runs "fine" - except the behavior is totally unintended.
>>
>> It's evil in my opinion.
>
> C++ default parameters are evil, indeed.
>
> A better design for default parameters would be leaving the parameter
> empty, which would make sure you don't misplace things.
>
> f(f1(2, p),);
>
> That extra comma makes sure you know what you're doing, by explicitly
> asking for the default value. I do default parameters in C like that,
> which can be easily implemented with macros.
>
> In general, I would recommend avoiding default parameters in C++.
You can usually use overloads to avoid default parameters :
void f(int a, bool b);
void f(int a) { f(a, false); }
int f1(int a, int * p);
int f1(int a) { return f1(a, nullptr); }
It does not help the OP's problem in this case (the cause is the
implicit conversion of pointers to bool, not the default parameters).
But it avoids another aspect of default parameters that I dislike - the
loss of symmetry between declarations (with the default value) and
definitions (which must not have the default value).
Received on 2026-04-20 13:12:12
