home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #31 / NN_1992_31.iso / spool / comp / std / c / 3267 < prev    next >
Encoding:
Text File  |  1992-12-24  |  5.0 KB  |  111 lines

  1. Newsgroups: comp.std.c
  2. Path: sparky!uunet!haven.umd.edu!decuac!pa.dec.com!e2big.mko.dec.com!jrdzzz.jrd.dec.com!jit533.jit.dec.com!diamond
  3. From: diamond@jit533.jit.dec.com (Norman Diamond)
  4. Subject: Re: Compatability of enum types and integer types
  5. Message-ID: <BzrA9D.KrL@jrd.dec.com>
  6. Sender: usenet@jrd.dec.com (USENET News System)
  7. Nntp-Posting-Host: jit533.jit.dec.com
  8. Reply-To: diamond@jit.dec.com (Norman Diamond)
  9. Organization: Digital Equipment Corporation Japan , Tokyo
  10. References: <1992Dec21.225604.18749@netcom.com> <1992Dec22.203938.29580@lucid.com> <Bzn05H.nq@jrd.dec.com> <20361@ksr.com>
  11. Date: Thu, 24 Dec 1992 08:58:25 GMT
  12. Lines: 97
  13.  
  14. In article <1992Dec22.203938.29580@lucid.com> jss@lucid.com (Jerry Schwarz) writes:
  15. >In article <1992Dec21.225604.18749@netcom.com>, rfg@netcom.com (Ronald F. Guilmette) writes:
  16. >>The ANSI C standard says (in 3.5.2.2):
  17. >>    "Each enumerated type shall be compatible with an integer type;
  18. >>    the choice of type is implementation-defined."
  19.  
  20. >It requires more for
  21. >    enum E object ;
  22. >    int object ;
  23. >to be legal than that E and int be compatible.
  24.  
  25. >3.1.2.6 says that the "type of the identifier becomes the composite type".
  26.  
  27. Yes.
  28.  
  29. >It also gives the rules for determining a composite type and says nothing
  30. >about combining enums and integer types.  Granted, this isn't a constraint
  31. >section, but if there isn't a composite type then I think the compiler is
  32. >justified in treating the above as an error.
  33.  
  34. It says about ALL potential combinations, "it is a type that is compatible
  35. with both of the two types", and adds for some particular types, "and
  36. satisfies the following conditions".  (Page 26 lines 11 to 12.)
  37.  
  38. The same reasoning allows the following two declarations at file scope:
  39.     int object;
  40.     int object;
  41. and the following more obviously useful:
  42.     int object;
  43.     int object = 3;
  44. (and also versions with internal linkage).
  45.  
  46. In article <20361@ksr.com> jfw@ksr.com (John F. Woods) writes:
  47. >diamond@jit533.jit.dec.com (Norman Diamond) writes:
  48. >>Being implementation-defined means that it must be documented.  Regarding
  49. >>some other implementation-defined matters (e.g. which of two possible results
  50. >>is obtained from the % operator) most of the regulars in this newsgroup seem
  51. >>to feel that the implementation must define a result that is identical over
  52. >>all usages.  This kind of opinion suggests that the documentation must tell
  53. >>you which type you can use for `T'.
  54. >>My opinion is that the documentation can tell you that the implementation
  55. >>plays dirty pool.  However, I seem to be a minority of 1.
  56.  
  57. >I think you're a minority of two.  I can't see any way for a program to make
  58. >use of this in a way that exposes the type yet is guaranteed.
  59.  
  60. Well, the undetermined matter is just how thoroughly the type is exposed.
  61. Thank you for the support though!
  62.  
  63. >I think one can argue that
  64. >    enum thing { small, big=INT_MAX };
  65. >is required to be compatible with a type no shorter than int,
  66.  
  67. How do you argue this?  I cannot find any support for it in the standard.
  68.  
  69. >I think a [hypothetical implementation's] definition of
  70. >    Enumeration types are compatible with "int", unless an object in a
  71. >    translation unit is declared as an enumeration type and as an "int"
  72. >    in the same translation unit or a different translation unit at link
  73. >    link time, in which case enumeration types are compatible with "long".
  74. >meets all the requirements present in the standard.
  75.  
  76. I agree.  But by analogy again, I thought a hyp. imp.'s definition of
  77.     The result of % with one or two negative operands in the expression
  78.     (a/b)*b + a%b is appropriate so that the expression will equal a,
  79.     but in any other expression will alternate between permissible choices.
  80. also meets all the requirements present in the standard, but no one agreed.
  81.  
  82. >I think it's pretty clear that the type compatibility of enumerations need
  83. >*not* be identical over all usages:
  84.  
  85. Yup, page 62 line 39.
  86.  
  87. >It might be argued, however, that since the Standard could merely have said
  88. >that enumerations are integral types, but are not *compatible* with any
  89. >integral types, that there is an implicit requirement that one be able to
  90. >take advantage of that type compatibility.
  91.  
  92. ANSI Classic 3.1.2.5, page 25 lines 1 to 2, includes enumerated types in the
  93. category of integral types.
  94.  
  95. >I think the compatibility requirement only buys one the ability to pass
  96. >enumerations to functions expecting integral types with some hope of
  97. >portability (though I suspect that if enumerations can be unsigned types,
  98. >it opens an even larger can of worms).
  99.  
  100. In order to pass enumerations to functions, you'd better make sure that the
  101. same enumeration type is declared for the receiving function.  (My wording
  102. is lazy; see ANSI Classic 3.1.2.6, page 26 lines 2 to 8, for exact wording.)
  103. Of course, if you want to write implementation-dependent code for an
  104. implementation that defines things nicely enough for you, you can do that.
  105.  
  106. I think enum types can be unsigned.  The can of worms is unchanged in size :-)
  107. --
  108. Norman Diamond       diamond@jit081.enet.dec.com
  109. If this were the company's opinion, I wouldn't be allowed to post it.
  110. "It's been a lovely recession."
  111.