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

  1. Path: sparky!uunet!olivea!spool.mu.edu!snorkelwacker.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: zero-length datatype
  5. Message-ID: <TMB.92Sep7171355@arolla.idiap.ch>
  6. Date: 7 Sep 92 21:13:55 GMT
  7. Sender: news@ai.mit.edu
  8. Reply-To: tmb@idiap.ch
  9. Distribution: comp
  10. Organization: IDIAP (Institut Dalle Molle d'Intelligence Artificielle
  11.     Perceptive)
  12. Lines: 45
  13.  
  14. C++ seems to be lacking in a data type that is guaranteed to be zero
  15. length.  For example, a variable cannot be declared to be of type
  16. "void":
  17.  
  18.     struct Foo {
  19.         int x;
  20.         void y;
  21.     };
  22.  
  23. Why would you want such a thing? With template classes, it happens not
  24. infrequently that some general class (e.g., a hash table) is useful
  25. when one of the types (for "dictionary-like" types, that is usually
  26. the "value" part) is unused. In that case, it would be nice if I could
  27. specify that that part should not take up any space.
  28.  
  29. Consider, for example, the following template class definition:
  30.     
  31.     template <class A,class B>
  32.     struct Bar {
  33.         A x;
  34.         B y;
  35.     };
  36.     
  37.  
  38. Now, the straightforward declaration obviously fails.
  39.  
  40.     Bar<int,void> s;
  41.  
  42. The following one compiles, but, unfortunately, "sizeof t" turns out
  43. to be 8, not 4 (I believe the C++ definition makes an effort to ensure
  44. that this is so):
  45.  
  46.     struct nothing {};
  47.     Bar<int,nothing> t;
  48.     
  49. The non-existence of a zero length type is somewhat of a nuisance.
  50. One fix might be to allow definitions of objects of type "void", give
  51. them zero length, and introduce a single value for objects of type
  52. "void", denoted "void()". Of course, there would be some minor
  53. conflicts with the "function(void)" syntax, but those could be
  54. resolved compatibly. Alternatively, another built-in special type
  55. could be introduced, say, called "unit", that would be available
  56. (only) after "#include <unit.h>".
  57.  
  58.                     Thomas.
  59.