home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #31 / NN_1992_31.iso / spool / comp / lang / cplus / 18523 < prev    next >
Encoding:
Text File  |  1992-12-29  |  10.7 KB  |  213 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: <1992Dec29.222453.7209@microsoft.com>
  6. Date: 29 Dec 92 22:24:53 GMT
  7. Organization: Microsoft Corporation
  8. References: <BzFM01.CD@frumious.uucp> <1992Dec23.214139.24948@microsoft.com> <DAVEG.92Dec24193847@synaptx.synaptics.com>
  9. Lines: 202
  10.  
  11. In article <DAVEG.92Dec24193847@synaptx.synaptics.com> daveg@synaptics.com (Dave Gillespie) writes:
  12. |In article <1992Dec23.214139.24948@microsoft.com> jimad@microsoft.com (Jim Adcock) writes:
  13. |I have to disagree here.  Usually when language standards talk about
  14. |the "meaning" of a program, they are referring only to whether or not
  15. |it is correct, and what its behavior will be if it is correct.  As I
  16. |understand it, all the talk about macros in the C standard is there
  17. |purely to reserve to implementors the right to provide fast macro
  18. |implementations of library functions.
  19.  
  20. Again, section 4.1.6 of X3.159-1989 calls out explicit rights not
  21. only of implementors, but also of programmers.  Implementors have
  22. the right to use macro implementations, but also have to provide
  23. function implementations "in the background."  Programmers have the
  24. right to bypass the macro implementation [if any] using the "background" 
  25. function-call implementation. The ANSI-C++ committee intends to
  26. usurp both the rights of the implementor and the rights of the 
  27. programmer on this matter.  Why?  There is NO REASON why the ANSI-C++
  28. committee has to use the ANSI-C libraries if they don't like the ANSI-C
  29. library rules.  Let the ANSI-C++ committee define their own libraries,
  30. with the own *differently named include headers*, if they do not like
  31. the way the ANSI-C libraries are defined to work.  That way the
  32. programmer could choose whose set of rules they agreed with --
  33. ANSI-C or ANSI-C++ -- and more importantly existing historical 
  34. agreements between C implementors and C programmers would not be broken.
  35.  
  36. For example, the ANSI-C++ committee could decide they know better than
  37. ANSI-C how the ANSI-C headers ought to work, could name THEIR version
  38. of the header files <stdio.h++> [or something] and then the programmer
  39. could decide WHOSE header and set of rules to follow <stdio.h> or
  40. <stdio.h++>
  41.  
  42. I am not saying this is what SHOULD be done, I'm just saying there
  43. is no excuse for the ANSI-C++ committee to REQUIRE changes to the
  44. rules of how the ANSI-C libraries and their associated headers work.
  45. Because ANSI-C++ could simply NAME their headers differently if they
  46. are going to require that their headers WORK differently!
  47.  
  48. |If the only effect of `#undef putchar' is to change whether or not
  49. |a fast macro or a slow equivalent function is used, i.e., if it only
  50. |affects performance and not results, then I'd say it's improper for
  51. |a standard to legislate it one way or another, or for a portable
  52. |program to depend on it.
  53.  
  54. I agree.  Thus I am lobbying against the proposed ANSI-C++ mandated
  55. change.  If the ANSI-C++ doesn't mandate the implementation, then
  56. implementors can *continue* to implement standard C functions using
  57. macros.  Portable programs [ie "strictly conforming"] won't depend on this.
  58. But non-portable but still conforming programs CAN and DO depend upon
  59. this.  Remember, the vast majority of programs ARE NOT written to
  60. be portable, and it was never the intent of ANSI-C to *require*
  61. portability, but rather to *allow* portability to people who were
  62. so motivated.  On the contrary, ANSI-C clearly states that they
  63. consider *speed* a more important criteria than portability, and
  64. that programmers should not be hindered from writing fast, non-portable,
  65. programs.  Requiring inline implementation would be such a hindrance, because
  66. the reality remains that in some instances macros can still be
  67. faster than inlines.
  68.  
  69. |Even if `putchar' were a function instead of a macro, don't compilers
  70. |sometimes inline-expand functions anyway?  A good optimizing compiler
  71. |ought to be able to do this, whether or not there are "inline"
  72. |declarations in the language.  As far as I know, there is no way in
  73. |C to require that a function be compiled out-of-line.  The only
  74. |language I can think of offhand that does provide for this is Common
  75. |Lisp.
  76.  
  77. Various implementation have many different capabilities and implementations.
  78. What I am lobbying against is the proposed ANSI-C++ change requiring
  79. implementors NOT to use one historical implementation choice, namely
  80. the new proposed restriction requiring implementations to no longer
  81. use macro implementations of historical C standard functions.  Even
  82. though those macro implementations are clearly permitted and intended as
  83. stated in the ANSI-C standard.
  84.  
  85. |I don't have a copy of the standard handy, but my K&R II uses language
  86. |like "if it is a macro" in its description of "putchar".  Doesn't that
  87. |imply that it is perfectly legal for a compiler to implement `putchar'
  88. |as a function, not a macro?
  89.  
  90. Yes, and it implies that it is perfectly legal for a compiler to
  91. implement it as a macro, not a function.  And it is perfectly legal
  92. for a conforming program to expect its given implementation to provide
  93. this function in macro form, and to require that particular implementation
  94. for the "success" of that particular program.  Strictly-conforming
  95. programs are not allowed to have expectations on implementation beyond
  96. that called out in ANSI-C.  Conforming programs can have any expectations
  97. on a particular implementation that they want.  Such expectation represent
  98. private agreement between an implementor and a programmer using that
  99. implementation.  Now ANSI-C++ would override such private agreements.
  100.  
  101. |In fact, is there anything preventing a C implementation from putting
  102. |
  103. |    static int putchar(int c) { return putc(c, stdout); }
  104. |
  105. |in its <stdio.h> header?  Is there anything preventing a compiler
  106. |from expanding a call to "putchar" inline, given this definition?
  107. |(Urgh, I wish I had a copy of the standard around...)
  108.  
  109. Again, it is not an issue of "preventing" it is an issue of "requiring" --
  110. the proposed ANSI-C++ changes would require changes to existing
  111. implementations, breaking the agreements already established between
  112. that implementation and the expectations of conforming programs for
  113. that implementation.
  114.  
  115. |> Also, note that a conforming C implementation of these standard library
  116. |> functions using macros can accept a superset of the parm types that a
  117. |> C++ extern implementation or inline implementation can accept.
  118. |
  119. |Well, yes, but can a conforming, i.e., portable program depend on this?
  120.  
  121. You confuse conforming with strictly conforming.  A conforming program
  122. CAN make such expectations.  A strictly-conforming program cannot.
  123. Conforming and strictly conforming programs are BOTH "legal" programs.
  124. Neither is a "bad" program.  They might just meet differing requirements.
  125. For example, it is not uncommon for otherwise strictly-conforming
  126. programs to contain conforming sections surrounded by #ifdefs for
  127. various target implementations, such that the ultimate in performance
  128. can be achieved on each of those implementations.  This is "good".
  129. The programmer took such effort because such effort was needed.
  130. Such effort should be respected.  There should not be new requirements
  131. added in the name of "portability" that now defeats such efforts.
  132.  
  133. |My K&R says `int putchar(int c)'.  I have to assume that this is what
  134. |I have when I write a call to `putchar'.  Even if I have a whiz-bang
  135. |extended C compiler that allows me to pass other things to `putchar',
  136. |I have to assume any code that uses this will break when I move to
  137. |another compiler, be it a C++ compiler or just another vendor's C.
  138.  
  139. Yes.  Such a program would be conforming not strictly conforming.
  140. Again, there is nothing 'wrong' with conforming programs, they are
  141. just not very portable.  It was the expressed intent of C to allow
  142. BOTH programs that are not portable, and programs that ARE portable,
  143. the difference being in the effort and desires of the program writer.
  144. Frequently portability comes at an expense of speed, for example,
  145. or at the expense of writing [and generating] more code.
  146.  
  147. |> Thus, requiring that a macro *not* be
  148. |> used for implementation requires that some possible conforming programs
  149. |> will no longer be accepted.
  150. |
  151. |As I understand it, even if it's required for `putchar' and to `putc'
  152. |to be macros, it is still legal for their definitions to be
  153. |
  154. |    #define putc(c,f) fputc(c,f)
  155. |    #define putchar(c) putc(c,stdout)
  156. |
  157. |which means that a conforming program couldn't possibly pass anything
  158. |to `putchar' which it couldn't also pass to `fputc', which is certainly
  159. |allowed to be a function.
  160.  
  161. No.  Again you confuse conforming with strictly conforming.  What
  162. you say above is true of strictly conforming programs, not of conforming
  163. programs.
  164.  
  165. |What other properties of `putchar' being a macro or not could a
  166. |conforming program depend on?
  167. |
  168. |> The other side of the coin is that it then becomes impossible for
  169. |> programmers to specify *whether or not* they want an inline implementation,
  170. |> which is a stated intent of the original ANSI-C standard library 
  171. |> macro/function implementations.
  172. |
  173. |Could you please provide a quotation?
  174.  
  175. Again, section 4.1.6 of X3.159-1989
  176.  
  177. A tiny subpart says:
  178.  
  179. "Any macro definition of a function can be suppressed LOCALLY by
  180. enclosing the name of the function in parentheses, ...."
  181.  
  182. [emphasis mine]
  183.  
  184. Clearly the intent is that the programmer can CHOOSE where and when
  185. to use the macro implementation [fast speed] and  where and when
  186. to use the function implementations [small size] and the programmer
  187. can mix and match these choices as the *programmer* so desires.
  188.  
  189. Please read the whole paragraph if this remains unclear to anyone.
  190.  
  191. |> So then the next person has to somehow magically discover that the "meaning"
  192. |> of these "standard" functions have been changed from the C++ standard have
  193. |> been changed from the C standard....
  194. |
  195. |What has actually changed, that this next maintainer could perceive?
  196.  
  197. Speed verses size tradeoffs have been removed from the intended control
  198. of both the programmer and the implementor.  Thus the next maintainer says
  199. "Gosh darn, recompiling this under C++ made it 20% bigger and 2X as slow -- 
  200. clearly C++ s*cks the big one.  What do I do now?"
  201.  
  202. |Okay, the performance has changed some, but performance can change even
  203. |when you install a new rev of the "same" compiler.
  204.  
  205. But typically implementors are not REQUIRED to make their next generation
  206. compiler generate bigger slower code.  Customers do complain, and rightly
  207. so, when a next generation compiler does worse on their programs than
  208. their previous compiler.  The ANSI-C++ committee would now so require.
  209. I believe this would be a bad decision for C++.  What would happen is
  210. that C programs would simply continue to be maintained as C programs, 
  211. and would not be ported over to C++.  Because who wants bigger slower code?
  212.  
  213.