home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #20 / NN_1992_20.iso / spool / comp / lang / cplus / 13463 < prev    next >
Encoding:
Text File  |  1992-09-10  |  4.0 KB  |  114 lines

  1. Path: sparky!uunet!olivea!mintaka.lcs.mit.edu!ai-lab!life.ai.mit.edu!tmb
  2. From: tmb@arolla.idiap.ch (Thomas M. Breuel)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: zero-length datatype
  5. Message-ID: <TMB.92Sep10123201@arolla.idiap.ch>
  6. Date: 10 Sep 92 16:32:01 GMT
  7. References: <TMB.92Sep7171355@arolla.idiap.ch> <1992Sep10.003726.27140@lucid.com>
  8. Sender: news@ai.mit.edu
  9. Reply-To: tmb@idiap.ch
  10. Distribution: comp
  11. Organization: IDIAP (Institut Dalle Molle d'Intelligence Artificielle
  12.     Perceptive)
  13. Lines: 98
  14. In-reply-to: jss@lucid.com's message of 10 Sep 92 00:37:26 GMT
  15.  
  16. In article <1992Sep10.003726.27140@lucid.com> jss@lucid.com (Jerry Schwarz) writes:
  17.  
  18.    In article <TMB.92Sep7171355@arolla.idiap.ch>, tmb@arolla.idiap.ch (Thomas M. Breuel) writes:
  19.    |> C++ seems to be lacking in a data type that is guaranteed to be zero
  20.    |> length.
  21.  
  22.  
  23.    This understates the situation.  C++ guarantees that no object
  24.    will have size 0.
  25.  
  26. I pointed this out myself in my posting.
  27.  
  28.    For example 
  29.  
  30.        struct Z { } ;
  31.  
  32.    may not have size 0.
  33.  
  34.    One reason for this constraint is to ensure that no two objects
  35.    have the same address.  In particular the elements of an array
  36.    are always at different addresses.
  37.  
  38.        Z a[10] ;
  39.  
  40.    Without that constrain a[0],...,a[10] would all compare equal.
  41.  
  42. I think what you meant to say is that "without that constraint,
  43. a+0,...,a+10 would all compare equal", since even with that
  44. constraint, a[0],...,a[10] should all compare equal (if you define
  45. operator==), since they have the same value.
  46.  
  47. And, my answer is, so what? The usual identities for arithmetic
  48. would still be preserved. There is nothing intrinsically bad
  49. about "sizeof (Z) == 0".
  50.  
  51. In any case, I am not suggesting that the semantics of empty
  52. structures be changed in any way, but that a new type (which could
  53. coincide with "void" but need not) of zero length be introduced.  No
  54. existing code would be affected by my suggestion, since currently no
  55. code defines or uses such zero-length objects.
  56.  
  57.    The constraint predates templates. Maybe it should be reconsidered.
  58.    However that would require some consideration of the effect 
  59.    on existing code, and on common programming idioms.
  60.  
  61.    For example something like
  62.  
  63.        Z a[10] ;
  64.        ... 
  65.        count = sizeof(a)/sizeof(Z)
  66.  
  67.    suddenly becomes a divde by zero.
  68.  
  69. Well, "void" would pretty much only be used as an argument to a
  70. template. And, yes, no question about it, unsuspecting template code
  71. could behave in odd ways when given the zero-length type as an
  72. argument (this particular case happens to be statically detectable)
  73. (*).  But unsuspecting template code can also behave in odd ways or
  74. even fail to type check when given some other type as an argument that
  75. doesn't have quite the types or semantics that the template code
  76. expects. That's an intrinsic limitation of the C++ template system and
  77. unrelated to the existence of zero-length datatype.
  78.  
  79. There are, in fact, two issues: (1) should a zero-length datatype be
  80. introduced, and (2) could "void" be used to denote it.
  81.  
  82. I definitely believe in (1). I don't know whether (2) can be achieved
  83. without some snag, but "void" would be the logical notation for a zero-
  84. length datatype, and it might remove many of the seemingly arbitrary
  85. restrictions on what you can and cannot do with "void".
  86.  
  87.                     Thomas.
  88.  
  89. (*) Two examples:
  90.  
  91. The following terminates immediately, which is probably good:
  92.  
  93.     void a[10];
  94.     for(void *p = a;p<a+10;p++) ... 
  95.  
  96. The following never terminates, which is probably not so good:
  97.  
  98.     void a[10];
  99.     void *p = a+10;
  100.     while(--p>=a) ...
  101.  
  102. Template code that uses descending loops with pointers over an array
  103. of a template argument would simply not work if that template argument
  104. is "void". That's no different from lots of other failures that can
  105. occur with templates. The following loop, for example, may never
  106. terminate if the range of type T is too small (e.g., T is "char"):
  107.  
  108.     template <class T>
  109.     ... for(T x=0;x<1000;x++) ...
  110.  
  111. Similar problems occur if you use a floating point type instead of an
  112. integral type, or, in fact, if you use any type as a template argument
  113. that doesn't have quite the right semantics for the template class.
  114.