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

  1. Path: sparky!uunet!cs.utexas.edu!qt.cs.utexas.edu!yale.edu!yale!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: tagged unions, an alternative to RTTI (Re: run-time type checking)
  5. Message-ID: <TMB.92Jul30181326@arolla.idiap.ch>
  6. Date: 30 Jul 92 22:13:26 GMT
  7. References: <1992Jul24.234628.21196@cadsun.corp.mot.com>
  8.     <1992Jul25.172045.24675@ucc.su.OZ.AU> <BryL9q.K5I@watcgl.waterloo.edu>
  9.     <1992Jul28.183746.24287@ucc.su.OZ.AU>
  10.     <TMB.92Jul29125322@arolla.idiap.ch> <TMB.92Jul30152807@arolla.idiap.ch>
  11. Sender: news@ai.mit.edu
  12. Reply-To: tmb@idiap.ch
  13. Followup-To: comp.lang.c++
  14. Organization: IDIAP (Institut Dalle Molle d'Intelligence Artificielle
  15.     Perceptive)
  16. Lines: 38
  17. In-reply-to: tmb@arolla.idiap.ch's message of 30 Jul 92 19:28:07 GMT
  18.  
  19. Deviasse Robert N writes:
  20.  
  21.    Tagged unions may be simulated in C++ by the method described in "The C++ 
  22.    Programming language 2nd Ed" page 168.
  23.  
  24. Even in C, people have simulated tagged unions for a long time. In
  25. fact, unions have no interesting portable semantics unless you keep a
  26. tag somewhere. By making the tag explicit in the language and letting
  27. the compiler know about it, you can obtain the benefits of static
  28. and/or dynamic type checking resulting from explicit compiler support.
  29.  
  30.    [... messy example deleted ...]
  31.  
  32.    Yes, it's verbose, but this is the type of contortions that the compiler
  33.    has to go through if tagged unions were added to C++ -- this is probably
  34.    the reason why tagged unions were not introduced into C++.
  35.  
  36. Adding tagged unions to the C++ language would be a very minor
  37. extensions. There is nothing messy or complicated about it.
  38.  
  39.    (besides, what
  40.    is the error() function above, is it an exception, an assert(), ...)
  41.  
  42. You can, in fact, define tagged unions such that no use of a tagged
  43. union can result in a runtime type error. In this regard, tagged union
  44. are nicer than RTTI (in the current RTTI proposal, type errors show up
  45. as NULL pointers that you might forget to handle). This is also the
  46. reason why "simulations" of tagged unions are not as nice as language
  47. built-ins: simulations will have to raise runtime type errors for many
  48. problems that, in principle, could be diagnosed at compile time.
  49.  
  50. RTTI and tagged unions are not replacements for one another. However,
  51. tagged unions would be a safer alternative to RTTI in many cases. In
  52. particular, tagged unions would allow you to have heterogeneous
  53. collections of objects of arbitrary, unrelated types. I think it
  54. would be nice to have them in the language.
  55.  
  56.                     Thomas.
  57.