home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #1 / NN_1993_1.iso / spool / comp / lang / cplus / 19030 < prev    next >
Encoding:
Text File  |  1993-01-12  |  1.5 KB  |  39 lines

  1. Newsgroups: comp.lang.c++
  2. Path: sparky!uunet!psinntp!cci632!dwr
  3. From: dwr@cci632.cci.com (Donald W. Rouse II)
  4. Subject: Re: "type safety" deemed essential
  5. Message-ID: <1993Jan12.180548.22627@cci632.cci.com>
  6. Organization: [Computer Consoles, Inc., Rochester, NY
  7. References: <725053473snx@trmphrst.demon.co.uk> <726346306snx@trmphrst.demon.co.uk> <TMB.93Jan6220042@arolla.idiap.ch>
  8. Date: Tue, 12 Jan 1993 18:05:48 GMT
  9. Lines: 28
  10.  
  11. In article <TMB.93Jan6220042@arolla.idiap.ch> tmb@idiap.ch writes:
  12. > [...]
  13. >Unfortunately, C++ still lacks some kind of "tagged union type",
  14. >"typesafe union type", or, if you are a Pascal fan, a "variant record
  15. >with runtime type checking". Those kinds of datatypes are useful in
  16. >many applications. A common example is that of a binary tree with
  17. >different node and leaf types.
  18. >
  19. >There are workarounds, but they either use a lot more memory (using a
  20. >"struct" instead of a "union"), or they aren't type-safe (pointer
  21. >casts or C-style unions). And with any such workaround in C/C++, you
  22. >are missing out on useful compile time diagnostics and optimizations
  23. >that would be possible if the feature was supported.
  24. >
  25. >                    Thomas.
  26.  
  27. True, but the type-unsafe stuff can be encapsulated:
  28.  
  29. class i_or_f
  30.     {
  31.     int isint;
  32.     union { int i; float f; };
  33.     public:
  34.         i_or_f (int ii) { isint = 1; i = ii; }
  35.         i_or_f (float ff) { isint = 0; f = ff; }
  36.         int geti (int* ip) { if (isint) *ip = i; return isint; }
  37.         int getf (float* fp) { if (!isint) *fp = f; return !isint; }
  38.     };
  39.