Topic: Can Standard C functions be overloaded?


Author: jamshid@emx.cc.utexas.edu (Jamshid Afshar)
Date: 11 Nov 92 02:14:41 GMT
Raw View
Can Standard C functions be overloaded?

 #include <string.h>
 int atoi(const String&);   // legal?
 char* s = "123";
 String str = "456";
 int i1 = atoi(s);
 int i2 = atoi(str);

One problem, I believe, is that ANSI C allows functions to be defined
by the implementation as macros, so you might have to do:

 int (atoi)(const String& s);  // now legal?
 String str = "456";
 int i = (atoi)(str);   // annoying

Jamshid Afshar
jamshid@emx.utexas.edu




Author: steve@taumet.com (Steve Clamage)
Date: Wed, 11 Nov 1992 20:01:31 GMT
Raw View
jamshid@emx.cc.utexas.edu (Jamshid Afshar) writes:


>Can Standard C functions be overloaded?

> #include <string.h>
> int atoi(const String&);   // legal?
> char* s = "123";
> String str = "456";
> int i1 = atoi(s);
> int i2 = atoi(str);

>One problem, I believe, is that ANSI C allows functions to be defined
>by the implementation as macros, so you might have to do:

> int (atoi)(const String& s);  // now legal?
> String str = "456";
> int i = (atoi)(str);   // annoying

The C++ Committee has adopted the following rules (among others)
concerning identifiers in the library section of the Standard:

 - No standard library function may be user-declared, but instead an
   appropriate header must be #include'd.
 - Any function declared in a header must be declared so as to allow
   it to be overloaded by the use of another signature.  (This means
   that a "function" can't be a macro but may be inline.)
 - All identifiers with external linkage are always reserved for use
   as identifiers with extern "C" linkage.
 - Each identifier with file scope is reserved for use as an identifier
   with file scope.
 - Each function signature is reserved for use with both extern "C" and
   extern "C++" linkages as a function signature with file scope in
   the same name space if any of its associated headers is included.

These rules allow an implementation to provide the Standard C functions
as either C or C++ functions with external or file scope (e.g. inline).
They also allow names of Standard C functions to be used as class
member names, or otherwise overloaded.

Of course, current implementations might have tighter restrictions,
since these rules exist only in the Committee Working Paper.

You are probably safe in using a Standard C function name as a C++
function name if you always enclose its name in parens (to avoid clashes
with macro names), and you have a Standard-conforming preprocessing phase.

"Overloading" the Standard C functions is likely to cause odd problems
in general, so for now I would advise against the practice.  (A later
programmer might not appreciate the subtleties and get C macros when
C++ functions were expected.)
--

Steve Clamage, TauMetric Corp, steve@taumet.com
Vice Chair, ANSI C++ Committee, X3J16