home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #26 / NN_1992_26.iso / spool / comp / std / cplus / 1542 < prev    next >
Encoding:
Text File  |  1992-11-11  |  2.5 KB  |  66 lines

  1. Newsgroups: comp.std.c++
  2. Path: sparky!uunet!taumet!steve
  3. From: steve@taumet.com (Steve Clamage)
  4. Subject: Re: Can Standard C functions be overloaded?
  5. Message-ID: <1992Nov11.200131.5704@taumet.com>
  6. Organization: TauMetric Corporation
  7. References: <83359@ut-emx.uucp>
  8. Date: Wed, 11 Nov 1992 20:01:31 GMT
  9. Lines: 55
  10.  
  11. jamshid@emx.cc.utexas.edu (Jamshid Afshar) writes:
  12.  
  13.  
  14. >Can Standard C functions be overloaded?
  15.  
  16. >    #include <string.h>
  17. >    int atoi(const String&);   // legal?
  18. >    char* s = "123";
  19. >    String str = "456";
  20. >    int i1 = atoi(s);
  21. >    int i2 = atoi(str);
  22.  
  23. >One problem, I believe, is that ANSI C allows functions to be defined
  24. >by the implementation as macros, so you might have to do:
  25.  
  26. >    int (atoi)(const String& s);  // now legal?
  27. >    String str = "456";
  28. >    int i = (atoi)(str);   // annoying
  29.  
  30. The C++ Committee has adopted the following rules (among others)
  31. concerning identifiers in the library section of the Standard:
  32.  
  33.  - No standard library function may be user-declared, but instead an
  34.    appropriate header must be #include'd.
  35.  - Any function declared in a header must be declared so as to allow
  36.    it to be overloaded by the use of another signature.  (This means
  37.    that a "function" can't be a macro but may be inline.)
  38.  - All identifiers with external linkage are always reserved for use
  39.    as identifiers with extern "C" linkage.
  40.  - Each identifier with file scope is reserved for use as an identifier
  41.    with file scope.
  42.  - Each function signature is reserved for use with both extern "C" and
  43.    extern "C++" linkages as a function signature with file scope in
  44.    the same name space if any of its associated headers is included.
  45.  
  46. These rules allow an implementation to provide the Standard C functions
  47. as either C or C++ functions with external or file scope (e.g. inline).
  48. They also allow names of Standard C functions to be used as class
  49. member names, or otherwise overloaded.
  50.  
  51. Of course, current implementations might have tighter restrictions,
  52. since these rules exist only in the Committee Working Paper.
  53.  
  54. You are probably safe in using a Standard C function name as a C++
  55. function name if you always enclose its name in parens (to avoid clashes
  56. with macro names), and you have a Standard-conforming preprocessing phase.
  57.  
  58. "Overloading" the Standard C functions is likely to cause odd problems
  59. in general, so for now I would advise against the practice.  (A later
  60. programmer might not appreciate the subtleties and get C macros when
  61. C++ functions were expected.)
  62. -- 
  63.  
  64. Steve Clamage, TauMetric Corp, steve@taumet.com
  65. Vice Chair, ANSI C++ Committee, X3J16
  66.