home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #16 / NN_1992_16.iso / spool / comp / lang / cplus / 11202 < prev    next >
Encoding:
Text File  |  1992-07-20  |  3.8 KB  |  110 lines

  1. Path: sparky!uunet!synaptx!synaptics.com!daveg
  2. From: daveg@synaptics.com (Dave Gillespie)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: boolean (was: Re: typedef vs enum)
  5. Message-ID: <DAVEG.92Jul20220553@synaptx.synaptics.com>
  6. Date: 21 Jul 92 05:05:52 GMT
  7. References: <DOUGM.92Jul19222728@titan.cs.rice.edu>
  8. Sender: daveg@synaptx.Synaptics.Com
  9. Organization: Synaptics, Inc., San Jose, CA
  10. Lines: 97
  11. In-reply-to: dougm@titan.cs.rice.edu's message of 20 Jul 92 04:27:28 GMT
  12.  
  13. In article <DOUGM.92Jul19222728@titan.cs.rice.edu> dougm@titan.cs.rice.edu (Doug Moore) writes:
  14. Andrew>     if (t!=0) cout << "Balderdash!\n";
  15. > You are right.  I was rash.
  16. > I am stung by your balderdash.
  17. > But if one bang just won't do,
  18. > Let's instead just make it two.
  19. >   In other words, try to find an anomaly with literals t!! and f!!.
  20.  
  21. [insert poetry here to taste]
  22.  
  23. If you define "t!!" as a "t" token followed by two "!" tokens, using
  24. the existing C++ lexical rules, then you still have the same sort of
  25. problem:  "if (t!!==2)" cannot be scanned properly (not left-to-right,
  26. anyway).
  27.  
  28. If you define "t!!" as a single token, may the fleas of a thousand
  29. camels, well, you get the idea.  :-)
  30.  
  31. Let's do away with t!! and f!! altogether:  The expressions !!1 and
  32. !!0 are just as simple, and we get them for free.  I find them easier
  33. to tell apart at a glance, too.
  34.  
  35.  
  36. Under "advantages," you might list the ability of a class to define
  37. "operator boole()" conversions instead of/as well as "operator int()".
  38. Many classes define misleading "int" conversions when they really
  39. want only a boolean conversion.
  40.  
  41. I worried for a moment about boolean bit-fields, but then I remembered
  42. that C++ allows bit-fields of any integral type (unlike ANSI C).
  43. That's good.
  44.  
  45. Is it possible for a template "Array<boole>" to define itself
  46. naturally as a packed bit vector?  That would be pretty cool.
  47.  
  48. Does there need to be any mention of "signed", "unsigned", "short",
  49. and "long" as applied to "boole"?  (Specifically, that all of these
  50. are disallowed?)
  51.  
  52.  
  53. One thing I worry about with this proposal is the ability to write
  54. programs to work equally well with pre- and post-"boole" C++.  I don't
  55. know what preprocessor name is being used to identify ANSI C++, but
  56. suppose it's __STDCPLUS__.  I thought about putting a simple
  57.  
  58. #ifndef __STDCPLUS__
  59. typedef int boole;
  60. // or: #define boole int
  61. #endif
  62.  
  63. at the front of my program, but that's not enough:  There will be
  64. situations where in ANSI C++ I want to overload like this,
  65.  
  66. class Foo {
  67.   void bar(int x) { ... }
  68.   void bar(boole x) { ... }
  69.   operator int() { ... }
  70.   operator boole() { ... }
  71. };
  72.  
  73. the typedef trick won't work because now we have multiple conflicting
  74. declarations when we use an old-style compiler.  Every one of these
  75. would also have to be guarded by #ifdef's.
  76.  
  77. What can be done to ease the transition from current C++ to C++
  78. with booleans?
  79.  
  80.  
  81. I like the way the boolean type solves all the ills of the "&&=/||="
  82. proposals.  There is no issue of normalizing the values to 0/1, because
  83. we know we're working with booleans already.  Very nice.
  84.  
  85. > The behavior of an expression of the form E1 &&= E2 is equivalent to
  86. > E1 ? (E1=(E2)) : f!, except that E1 is evaluated only once.
  87.  
  88. Any reason you wrote it that way instead of as "E1 = E1 && E2"?  The
  89. only way I can see that this would make a difference is if "E1" is a
  90. volatile variable.  Is that rare case worth sacrificing the elegance
  91. of defining "E1 <op>= E2" as "E1 = E1 <op> E2" always?
  92.  
  93.  
  94. There is a minor typo in section 1 of your proposal:
  95.  
  96. > When arithmetic or pointer types appear in a
  97. > context that requires a boolean value, standard logical conversions
  98. > are applied to convert nonzero arithmetic and nonnull pointer types to
  99. > f!.
  100.  
  101. This should be t! (or, possibly, !!1).
  102.  
  103.                                 -- Dave
  104. --
  105. Dave Gillespie
  106.   daveg@synaptics.com, uunet!synaptx!daveg
  107.   or: daveg@csvax.cs.caltech.edu
  108.