Article View: net.lang.c++
Article #86Function overloading
From: Bjarne Stroustrup
Date: Thu, 13 Mar 1986 01:57
Date: Thu, 13 Mar 1986 01:57
55 lines
2350 bytes
2350 bytes
> Path: alice!allegra!ulysses!bellcore!decvax!decwrl!sun!rmarti
> From: rmarti@sun.uucp (Bob Marti)
> Subject: Function Overloading
> Organization: Sun Microsystems, Inc.
(The following is slightly edited to save space)
> Assume the following declarations:
>
> int f(int x) { /* some code */ }
> overload f;
> float f(int x) { /* some code */ }
>
> and the following call of function f:
>
> int i;
> (void) f(i);
>
> Which function will be called, the one corresponding to int f(int x) or the
> one corresponding to float f(int x)?
Firstly, this example will not compile; you get:
"", line 3: error: f redefined as overloaded
The overload declaration for a name must occur before the first declaration
of a function with that name. I believe that this restriction avoids
unnecessary subtleties. So you might try this:
overload f;
int f(int x) { /* some code */ }
float f(int x) { /* some code */ }
This will not compile either; you get:
"", line 3: error: two different return value types for overloaded f(): int and float
C++ distinguishes overloaded functions based on the argument types only
(and never from context based on the return value). This implies that the
problem you appear to have in mind cannot occur. Overloaded function
resolution in C++ is a strictly bottom up affair (one function or operator
at a time). I beleive this makes it inherently simpler, faster to compile,
and easier to understand than, for example, Ada's rules.
What you ``lose'' in this scheme is the ability to have the declarations
above and then say
int i = f(2); // call ``int f(int)'' // this is not C++
int f = f(2); // call ``float f(int)'' // this is not C++
and then having to wonder about this one
(void) f(2); // which f?
- Bjarne Stroustrup (AT&T Bell Labs, Murray Hill)
Technical Details
Message-ID:
<5116@alice.uUCp>Path:
arc1-ca.pugleaf.net!utzoo!watmath!clyde!burl!ulysses!allegra!alice!bsShow Full Headers
Complete Article Headers
Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Posting-Version: version B 2.10.1 6/24/83; site alice.UUCP Path: arc1-ca.pugleaf.net!utzoo!watmath!clyde!burl!ulysses!allegra!alice!bs From: bs@alice.UucP (Bjarne Stroustrup) Newsgroups: net.lang.c++ Subject: Function overloading Message-ID: <5116@alice.uUCp> Date: Wed, 12-Mar-86 20:57:39 EST Article-I.D.: alice.5116 Posted: Wed Mar 12 20:57:39 1986 Date-Received: Fri, 14-Mar-86 04:53:26 EST Organization: Bell Labs, Murray Hill Lines: 55