home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!cs.utexas.edu!qt.cs.utexas.edu!yale.edu!yale!mintaka.lcs.mit.edu!ai-lab!life.ai.mit.edu!tmb
- From: tmb@arolla.idiap.ch (Thomas M. Breuel)
- Newsgroups: comp.lang.c++
- Subject: Re: tagged unions, an alternative to RTTI (Re: run-time type checking)
- Message-ID: <TMB.92Jul30181326@arolla.idiap.ch>
- Date: 30 Jul 92 22:13:26 GMT
- References: <1992Jul24.234628.21196@cadsun.corp.mot.com>
- <1992Jul25.172045.24675@ucc.su.OZ.AU> <BryL9q.K5I@watcgl.waterloo.edu>
- <1992Jul28.183746.24287@ucc.su.OZ.AU>
- <TMB.92Jul29125322@arolla.idiap.ch> <TMB.92Jul30152807@arolla.idiap.ch>
- Sender: news@ai.mit.edu
- Reply-To: tmb@idiap.ch
- Followup-To: comp.lang.c++
- Organization: IDIAP (Institut Dalle Molle d'Intelligence Artificielle
- Perceptive)
- Lines: 38
- In-reply-to: tmb@arolla.idiap.ch's message of 30 Jul 92 19:28:07 GMT
-
- Deviasse Robert N writes:
-
- Tagged unions may be simulated in C++ by the method described in "The C++
- Programming language 2nd Ed" page 168.
-
- Even in C, people have simulated tagged unions for a long time. In
- fact, unions have no interesting portable semantics unless you keep a
- tag somewhere. By making the tag explicit in the language and letting
- the compiler know about it, you can obtain the benefits of static
- and/or dynamic type checking resulting from explicit compiler support.
-
- [... messy example deleted ...]
-
- Yes, it's verbose, but this is the type of contortions that the compiler
- has to go through if tagged unions were added to C++ -- this is probably
- the reason why tagged unions were not introduced into C++.
-
- Adding tagged unions to the C++ language would be a very minor
- extensions. There is nothing messy or complicated about it.
-
- (besides, what
- is the error() function above, is it an exception, an assert(), ...)
-
- You can, in fact, define tagged unions such that no use of a tagged
- union can result in a runtime type error. In this regard, tagged union
- are nicer than RTTI (in the current RTTI proposal, type errors show up
- as NULL pointers that you might forget to handle). This is also the
- reason why "simulations" of tagged unions are not as nice as language
- built-ins: simulations will have to raise runtime type errors for many
- problems that, in principle, could be diagnosed at compile time.
-
- RTTI and tagged unions are not replacements for one another. However,
- tagged unions would be a safer alternative to RTTI in many cases. In
- particular, tagged unions would allow you to have heterogeneous
- collections of objects of arbitrary, unrelated types. I think it
- would be nice to have them in the language.
-
- Thomas.
-