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