home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!olivea!spool.mu.edu!snorkelwacker.mit.edu!ai-lab!life.ai.mit.edu!tmb
- From: tmb@arolla.idiap.ch (Thomas M. Breuel)
- Newsgroups: comp.lang.c++
- Subject: zero-length datatype
- Message-ID: <TMB.92Sep7171355@arolla.idiap.ch>
- Date: 7 Sep 92 21:13:55 GMT
- Sender: news@ai.mit.edu
- Reply-To: tmb@idiap.ch
- Distribution: comp
- Organization: IDIAP (Institut Dalle Molle d'Intelligence Artificielle
- Perceptive)
- Lines: 45
-
- C++ seems to be lacking in a data type that is guaranteed to be zero
- length. For example, a variable cannot be declared to be of type
- "void":
-
- struct Foo {
- int x;
- void y;
- };
-
- Why would you want such a thing? With template classes, it happens not
- infrequently that some general class (e.g., a hash table) is useful
- when one of the types (for "dictionary-like" types, that is usually
- the "value" part) is unused. In that case, it would be nice if I could
- specify that that part should not take up any space.
-
- Consider, for example, the following template class definition:
-
- template <class A,class B>
- struct Bar {
- A x;
- B y;
- };
-
-
- Now, the straightforward declaration obviously fails.
-
- Bar<int,void> s;
-
- The following one compiles, but, unfortunately, "sizeof t" turns out
- to be 8, not 4 (I believe the C++ definition makes an effort to ensure
- that this is so):
-
- struct nothing {};
- Bar<int,nothing> t;
-
- The non-existence of a zero length type is somewhat of a nuisance.
- One fix might be to allow definitions of objects of type "void", give
- them zero length, and introduce a single value for objects of type
- "void", denoted "void()". Of course, there would be some minor
- conflicts with the "function(void)" syntax, but those could be
- resolved compatibly. Alternatively, another built-in special type
- could be introduced, say, called "unit", that would be available
- (only) after "#include <unit.h>".
-
- Thomas.
-