home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #31 / NN_1992_31.iso / spool / comp / lang / cplus / 18374 < prev    next >
Encoding:
Text File  |  1992-12-23  |  4.7 KB  |  92 lines

  1. Newsgroups: comp.lang.c++
  2. Path: sparky!uunet!microsoft!hexnut!jimad
  3. From: jimad@microsoft.com (Jim Adcock)
  4. Subject: Re: Standard library functions and macros
  5. Message-ID: <1992Dec23.214139.24948@microsoft.com>
  6. Date: 23 Dec 92 21:41:39 GMT
  7. Organization: Microsoft Corporation
  8. References: <BzCFyM.2r5@frumious.uucp> <1992Dec16.185914.664@microsoft.com> <BzFM01.CD@frumious.uucp>
  9. Lines: 81
  10.  
  11. In article <BzFM01.CD@frumious.uucp> uunet.ca!frumious!pat writes:
  12. |Suppose the C++ standard said that standard library functions must
  13. |have external linkage, and prohibited implementing them as macros.
  14. |Would this change the meaning of any C programs if compiled as C++?
  15.  
  16. Yes, assuming by "the meaning" of a C program you mean "the intent
  17. of the original C author."  For example, the original author can
  18. do a '#undef putchar' with "the meaning" that in this compilation his/her
  19. intent is that putchar be implemented out-of-line.  Under the proposed
  20. required changes to C++ implementations, "the meaning" of the original
  21. C author would be silently changed.  Thus someone porting this C program
  22. to C++ finds [or worse, DOESN'T FIND!] that C++ silently changes the 
  23. intent of the original author.
  24.  
  25. Also, note that a conforming C implementation of these standard library
  26. functions using macros can accept a superset of the parm types that a
  27. C++ extern implementation or inline implementation can accept.  A macro
  28. implementation is somewhat analogous to the C++ situation where a 
  29. templated function is used.  Thus, requiring that a macro *not* be
  30. used for implementation requires that some possible conforming programs
  31. will no longer be accepted.  Still, breaking such a program is a MUCH better
  32. situation that silently accepting a program while changing "the meaning" 
  33. of that program.
  34.  
  35. Since its MUCH better to break a program than to silently change its meaning,
  36. a better "solution" to this problem would be to change all the "C" standard
  37. library function names to something new in C++.  Then an attempted call of
  38. a standard library funcion/macro would be guaranteed to cause a compilation
  39. error, rather than a silent change in meaning.  The porter would then simply
  40. have to find and fix all these compilation errors.
  41.  
  42. Now if such approach sounds too onerous to you -- then I agree -- leave
  43. "the meaning" of C standard library macro/functions ALONE!  Don't change 
  44. "the meaning" of the C standard library functions and ASSUME your silent
  45. changes are going to work.  They won't always work, and porters will be 
  46. stuck trying to figure out what has changed where without having ANY
  47. diagnostics to help them figure out what the problem is!
  48.  
  49. |Under this rule, it would still be possible (even easy) for compilers
  50. |to substitute inline code for most calls to certain library
  51. |functions. 
  52.  
  53. The other side of the coin is that it then becomes impossible for
  54. programmers to specify *whether or not* they want an inline implementation,
  55. which is a stated intent of the original ANSI-C standard library 
  56. macro/function implementations.
  57.  
  58. |As Steve Clamage pointed out, you can call the function through a
  59. |function pointer; this will probably cause the compiler to
  60. |generate a true function call.
  61.  
  62. I'd say possibly, not probably.  Optimizing C++ compilers are becoming
  63. better and better at figuring out and avoid gratuituous extra indirections.
  64.  
  65. |Going the other way, if the _implementation_ is prohibited from
  66. |making putchar a macro, the _programmer_ should be permitted to
  67. |make putchar a macro.  So the programmer who wants to force
  68. |inline code for putchar can simply write the macro.  A particular
  69. |implementation might choose to make this easier by providing
  70. |a __putchar macro so the programmer could write
  71. |
  72. |   #define putchar(c) __putchar(c)
  73.  
  74. This doesn't help someone who is trying to port someone else's C code,
  75. because such code is silently accepted with changed "meaning" so the
  76. porter *never even knows* that this #define is needed to maintain original
  77. intent.  Secondly, given the porter *does* put in such a #define, then
  78. the next person to maintain this code after the porter, is stuck with
  79. discovering that the first porter redefined the C++ meaning of these
  80. functions to be the same as the original C meaning of these functions.
  81. So then the next person has to somehow magically discover that the "meaning"
  82. of these "standard" functions have been changed from the C++ standard have
  83. been changed from the C standard....
  84.  
  85. I'd counterpropose why don't we all just *leave prior standards alone* unless
  86. we have overwhelming reasons to change them.  The standard library function
  87. names in C are reserved.  Leave them reserved, and leave them their C 
  88. "meanings".  There is no more reason why C++ people ought to be dinking
  89. with these reserved words than any of the other reserved words from 
  90. C, such as "for" "if" "else" "struct" etc.
  91.  
  92.