home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #31 / NN_1992_31.iso / spool / comp / lang / cplus / 18410 < prev    next >
Encoding:
Internet Message Format  |  1992-12-24  |  5.0 KB

  1. Path: sparky!uunet!synaptx!synaptics.com!daveg
  2. From: daveg@synaptics.com (Dave Gillespie)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Standard library functions and macros
  5. Message-ID: <DAVEG.92Dec24193847@synaptx.synaptics.com>
  6. Date: 25 Dec 92 03:38:47 GMT
  7. References: <BzCFyM.2r5@frumious.uucp> <1992Dec16.185914.664@microsoft.com>
  8.     <BzFM01.CD@frumious.uucp> <1992Dec23.214139.24948@microsoft.com>
  9. Sender: daveg@synaptx.Synaptics.Com
  10. Organization: Synaptics, Inc., San Jose, CA
  11. Lines: 97
  12. In-reply-to: jimad@microsoft.com's message of 23 Dec 92 21:41:39 GMT
  13.  
  14. In article <1992Dec23.214139.24948@microsoft.com> jimad@microsoft.com (Jim Adcock) writes:
  15. > In article <BzFM01.CD@frumious.uucp> uunet.ca!frumious!pat writes:
  16. > |Suppose the C++ standard said that standard library functions must
  17. > |have external linkage, and prohibited implementing them as macros.
  18. > |Would this change the meaning of any C programs if compiled as C++?
  19.  
  20. > Yes, assuming by "the meaning" of a C program you mean "the intent
  21. > of the original C author."  For example, the original author can
  22. > do a '#undef putchar' with "the meaning" that in this compilation his/her
  23. > intent is that putchar be implemented out-of-line.  Under the proposed
  24. > required changes to C++ implementations, "the meaning" of the original
  25. > C author would be silently changed.  Thus someone porting this C program
  26. > to C++ finds [or worse, DOESN'T FIND!] that C++ silently changes the 
  27. > intent of the original author.
  28.  
  29. I have to disagree here.  Usually when language standards talk about
  30. the "meaning" of a program, they are referring only to whether or not
  31. it is correct, and what its behavior will be if it is correct.  As I
  32. understand it, all the talk about macros in the C standard is there
  33. purely to reserve to implementors the right to provide fast macro
  34. implementations of library functions.
  35.  
  36. If the only effect of `#undef putchar' is to change whether or not
  37. a fast macro or a slow equivalent function is used, i.e., if it only
  38. affects performance and not results, then I'd say it's improper for
  39. a standard to legislate it one way or another, or for a portable
  40. program to depend on it.
  41.  
  42. Even if `putchar' were a function instead of a macro, don't compilers
  43. sometimes inline-expand functions anyway?  A good optimizing compiler
  44. ought to be able to do this, whether or not there are "inline"
  45. declarations in the language.  As far as I know, there is no way in
  46. C to require that a function be compiled out-of-line.  The only
  47. language I can think of offhand that does provide for this is Common
  48. Lisp.
  49.  
  50. I don't have a copy of the standard handy, but my K&R II uses language
  51. like "if it is a macro" in its description of "putchar".  Doesn't that
  52. imply that it is perfectly legal for a compiler to implement `putchar'
  53. as a function, not a macro?
  54.  
  55. In fact, is there anything preventing a C implementation from putting
  56.  
  57.     static int putchar(int c) { return putc(c, stdout); }
  58.  
  59. in its <stdio.h> header?  Is there anything preventing a compiler
  60. from expanding a call to "putchar" inline, given this definition?
  61. (Urgh, I wish I had a copy of the standard around...)
  62.  
  63. > Also, note that a conforming C implementation of these standard library
  64. > functions using macros can accept a superset of the parm types that a
  65. > C++ extern implementation or inline implementation can accept.
  66.  
  67. Well, yes, but can a conforming, i.e., portable program depend on this?
  68. My K&R says `int putchar(int c)'.  I have to assume that this is what
  69. I have when I write a call to `putchar'.  Even if I have a whiz-bang
  70. extended C compiler that allows me to pass other things to `putchar',
  71. I have to assume any code that uses this will break when I move to
  72. another compiler, be it a C++ compiler or just another vendor's C.
  73.  
  74. > Thus, requiring that a macro *not* be
  75. > used for implementation requires that some possible conforming programs
  76. > will no longer be accepted.
  77.  
  78. As I understand it, even if it's required for `putchar' and to `putc'
  79. to be macros, it is still legal for their definitions to be
  80.  
  81.     #define putc(c,f) fputc(c,f)
  82.     #define putchar(c) putc(c,stdout)
  83.  
  84. which means that a conforming program couldn't possibly pass anything
  85. to `putchar' which it couldn't also pass to `fputc', which is certainly
  86. allowed to be a function.
  87.  
  88. What other properties of `putchar' being a macro or not could a
  89. conforming program depend on?
  90.  
  91. > The other side of the coin is that it then becomes impossible for
  92. > programmers to specify *whether or not* they want an inline implementation,
  93. > which is a stated intent of the original ANSI-C standard library 
  94. > macro/function implementations.
  95.  
  96. Could you please provide a quotation?
  97.  
  98. > So then the next person has to somehow magically discover that the "meaning"
  99. > of these "standard" functions have been changed from the C++ standard have
  100. > been changed from the C standard....
  101.  
  102. What has actually changed, that this next maintainer could perceive?
  103. Okay, the performance has changed some, but performance can change even
  104. when you install a new rev of the "same" compiler.
  105.  
  106.                                 -- Dave
  107. --
  108. Dave Gillespie
  109.   daveg@synaptics.com, uunet!synaptx!daveg
  110.   or: daveg@csvax.cs.caltech.edu
  111.