Topic: defaults re-visited
Author: whipp@roborough.gpsemi.com (David Whipp.)
Date: Tue, 30 Aug 1994 08:07:16 GMT Raw View
The issue of default arguments to functions has been raised many times
in this group. I'd just like to add a few thoughts.
Defaults are a property of the interface to a function, not the
implementation. Unfortunately, This is not the case for all functions.
A function specified to have a variable number of arguments (final
argument is elipsis '...') can only use defaults if these are hard
coded into implementation. This is obvious because only the rightmost
arguments can be defaulted. Thus we have a group of function in which
we cannot use defaults.
The arm states that:
int f(int a, int b, int c=4);
is equivelent to:
int f(int a, int b, int c=4);
inline int f(int a, int b) { return f(a,b,c=4); }
This means that the two functions cannot be distinct. Similary, another
function:
int f(int a, int b, string c="four");
cannot be defined. While it is probably true that each of these problems
are probably not important (if a design runs into them then there's
probably a deeper problem), it does seem to me that the concept of
default arguments has not been fully considered.
My reading of the situation is that the position is: "Defaults are not
an important language feature. Current versions of c++ use them, so
we won't get rid of them. Because they're not important, we won't bother
to do them properly as to do so would break existing code."
Am I correct?
My proposed implementation of defaults would be to use the keyword: default.
As has been stated by a previous poster, defaults are used to avoid 'magic
numbers' in the code, not to reduce size of the source code. Requiring the
explicit use of a keyword where defaults are used would be less error prone
than the existing implementation. It would be orthogonal to the language,
instead of messing it up - "(int) default" and "(string) default" could be
used in the above example to avoid restricting overloading. Because
the keyword could be used anywhere within the function, not just for
rightmost arguments, there will no longer be a group of functions that
cannot use default arguments.
Unfortunately, getting the standard out quickly is probably more important
than fixing these minor details. However, If vendors were to implement
this system then it might find its way into the next version of the
standard.
--
David P. Whipp. <whipp@roborough.gpsemi.com>
Not speaking for: -------------------------------------------------------
G.E.C. Plessey Due to transcription and transmission errors, the views
Semiconductors expressed here may not reflect even my own opinions!