[Exceptional C++ Style] Re: [Effective C++ Style] Re: [Effective-cpp] Item 4: Standard library member functions

Lois Goldthwaite lois at loisgoldthwaite.com
Sat Nov 13 16:17:19 EST 2004


On Sunday 07 November 2004 21:25, Kevlin Henney wrote:
> In message <418E6EEC.4020207 at rubikon.pl>, Bronek Kozicki
> <brok at rubikon.pl> writes
>
> >Kevlin Henney wrote:
> >> I  am not sure that we would want to follow the path of making
> >>default  arguments -- or rather, I am sure than we don't want to
> >>follow that path!
> >
> >May I ask why?
>
> Well the general answer is: unnecessary complexity. If you work it
> through, they do not give you the effect that you want, which is to
> template on overload set -- if that's what is needed, then that is the
> solution and, based on what I believe is going on with aliasing
> (although I haven't followed it) that is the work to build on.

Remember also that default arguments can be added to a function in more than 
one place, and they have block scope -- see 8.3.6p4. So you can invoke the 
same function from different places and have different default arguments, and 
different numbers of default arguments, applied to the calls.

In practice, I fear that actually doing so would prove so confusing to the 
programmer that it's best to confine default arguments to the function 
definition only (if you must use them). And that is before any complications 
added by making them effective for pointers to functions!

>
> Default arguments are not one of the best ideas C++ ever had, and
> encouraging their use and complicating their syntax would, IMO, be
> precisely the wrong (in fact opposite) direction to take.

I think Bjarne has said that default arguments were a workaround to the 
difficulty that some very early compilers had with overloaded functions (and 
yes, children, in the olden days you had to tag overloaded functions with a 
keyword to help out the compiler). 

>
> Values are only part of a type when they are compile-time constants in
> templates. Functions with default arguments are first and foremost
> functions with the fully specified number of arguments. They describe
> the type of an actual function: a function declaration with defaulted
> arguments is not an actual function. They are not constrained to have
> compile-time evaluated arguments. And they are compatible with functions
> that take the full number of arguments.
>
> Having gone through the all the rounds about whether or not a throw spec
> is part of a function's type or not, reopening that whole can of worms
> does not appeal to me! It would be like pouring petrol rather than water
> on the fire :-}

A couple more citations I stumbled across looking up the above:

8.3.5p4 -- The return type, the parameter type list and the cv-qualifier-seq, 
but not the default arguments (dcl.fct.default) or the exception 
specification (except.spec), are part of the function type. [Note: function 
types are checked during the assignments and initializations of 
pointer-to-functions, reference-to-functions, and 
pointer-to-member-functions. ] 

and a footnote to 8.3.6p3 -- This means that default arguments cannot appear, 
for example, in declarations of pointers to functions, references to 
functions, or typedef declarations. 

Lois




More information about the Effective-cpp mailing list