style convention: /*foo_p=*/ to annotate bool arguments

Martin Sebor msebor@gmail.com
Tue Oct 4 20:00:00 GMT 2016


> 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



More information about the Gcc mailing list