On 04/10/16 22:00, Martin Sebor wrote:
>> This would have been easier if C++ had allowed the same default value to
>> be given in both the declaration and the definition:
>>
>> void foo(int x, int y, bool bar_p = false);
>>
>> void foo(int x, int y, bool bar_p = false)
>> {
>> }
>>
>> It seems strange that this is not allowed.  The standard says "A default
>> argument shall not be redefined by a later declaration (not even to the
>> same value)", but I can't think /why/ it is not allowed.
> 
> It seems like an innocuous and even useful feature to allow just
> as long as the default value is the same.  The perhaps surprising
> challenge is in defining "is the same."  E.g., in
> 
>   void foo (T = X + 1);
>   ...
>   void foo (T = 1 + X) { }
> 
> the result of the addition could be different depending on the type
> of T and the definition of operator+.  (The default value assigned
> to the parameter also depends on the type of T and the conversion
> from the result of the addition to it.)
> 
> A rule that required both declarations to evaluate to the same value
> would be a lot more involved than simply prohibiting a redeclaration.
> WG21 tried to nail the general problem down for C++ 2017 with
> the Defaulted Comparison Operators but ultimately decided it was too
> thorny of a problem to solve.
> 
> Martin

Thanks for that explanation.  Whenever I think "surely that would
simple?" in C++, there's always a way to make it more complicated!  I
guess that's the price to pay for the flexibility.  A compromise could
be made to allow the redeclaration in certain cases (compile-time
constants, or constexpr's perhaps) - but I can see how simply
prohibiting them is easier and more consistent.


Reply via email to