Lecture 5 Function Overloading
Lecture 5 Function Overloading
The compiler will actually go to a significant amount of effort to find a function to fulfill
a function call specified by the programmer. Given its list of functions with the same
name, it goes through the following sequence of steps according to the specification
outlined in the 2nd edition of Stroustrup's book:
This sequence is actually obsolete for most compilers. ANSI has drafted the definitive
one. It does give the approximate flavor of what is involved in overloading resolution.
At each level, the compiler looks for a match. If it finds one function that works, then
that's the one that's used and the search is over. If it can't find at least one, then it goes on
to the next level. If it finds more than one, it flags an ambiguity error. The rules are
chosen so that, when the choice is obvious, the compiler can choose the correct version of
the function to call without the programmer having to supply all the details of what needs
to be converted to what, and yet. when the programmer makes a mistake and codes a call
where the correct function to use isn't obvious, the compiler can spot this and flag an
ambiguity.
Ambiguity errors merely signify that the compiler needs more information about the
programmer's preferences to resolve the call. The programmer can do this by explicitly
doing the argument conversions necessary to resolve the ambiguity. Usually the culprit is
a conversion function, i.e. the ambiguity would not exist if the conversion function were
not there. This is no reason to avoid conversion functions on this account. One must
simply weigh the convenience of the automatic conversion against the inconvenience of
explicitly resolving ambiguities.
In resolving calls to overloaded member functions, no alterations are done on the implicit
first argument with the exception perhaps of some of the trivial conversions.
For linkers to be able to handle this system of function overloading, the names of
functions must usually be "mangled" before being handed on to the linker. The mangled
name is a new name which incorporates the old name as well as information encoded
about the arguments. Most debuggers don't require a programmer to know the mangled
name to access a variable. The mangled names would probably be encountered at an
assembly code level.
short si;
int i;
long li;
float f;
double d;
plus(i, i);
plus(si, si);
plus(li, i);
plus(f, d);
plus(li, d);