Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c++ > #6545
| From | Edek <edek.pienkowski@gmail.com> |
|---|---|
| Newsgroups | comp.lang.c++ |
| Subject | Re: unions |
| Date | 2011-06-10 13:16 +0200 |
| Organization | ATMAN - ATM S.A. |
| Message-ID | <issud7$q6k$1@node2.news.atman.pl> (permalink) |
| References | <iscfn1$keo$8@reader1.panix.com> <92e3c64e-d8e5-4dce-a994-0006f43c6416@35g2000prp.googlegroups.com> |
>
> The union definition is something like this (simplified):
>
> union xt {
> int i;
> void (*f)();
> char *c;
> xt *p;
> }
>
> Now, I understand this is absolutely undefined behavior not guaranteed
> to
> work on all compilers; but as a question of practicality, it does work
> on the compilers and systems I need it to work on.
I don't see why this should be undefined behaviour based on what you
wrote; I do not know how you intend to use the union, that may be
undefined, but in general using unions in C++ is not undefined
behaviour. As someone wrote in this thread before, if, for every
'instance', if you read the same thing as you wrote to it - ie.
write function pointer, read function pointer - it is just fine.
Otherwise there would be no point in using unions in C++, no?
I think the pattern could be:
struct {
int actualType;
union {
type1 _t1;
type2 _t2;
} val;
};
Then, based on actualType, you can read safely the right type,
assuming you set the actualType to some enum value which is appropriate.
Maybe actualType could be avoided if the 'type' of the union can
be determined from context, e.g. you don't dump the union to string,
and you don't need to double check the 'type' either.
Back to comp.lang.c++ | Previous | Next — Previous in thread | Find similar | Unroll thread
unions Ruben Safir <ruben@mrbrklyn.com> - 2011-06-04 05:26 +0000
Re: unions Ian Collins <ian-news@hotmail.com> - 2011-06-04 17:38 +1200
Re: unions Ruben Safir <ruben@mrbrklyn.com> - 2011-06-04 05:45 +0000
Re: unions Ian Collins <ian-news@hotmail.com> - 2011-06-04 18:04 +1200
Re: unions Ruben Safir <ruben@mrbrklyn.com> - 2011-06-04 06:13 +0000
Re: unions Öö Tiib <ootiib@hot.ee> - 2011-06-04 04:54 -0700
Re: unions Edek <edek.pienkowski@gmail.com> - 2011-06-04 19:59 +0200
Re: unions Vaclav Haisman <v.haisman@sh.cvut.cz> - 2011-06-04 20:13 +0200
Re: unions Edek <edek.pienkowski@gmail.com> - 2011-06-04 22:34 +0200
Re: unions Vaclav Haisman <v.haisman@sh.cvut.cz> - 2011-06-05 07:29 +0200
Re: unions Jorgen Grahn <grahn+nntp@snipabacken.se> - 2011-06-09 09:19 +0000
Re: unions Keith <kbelder@gmail.com> - 2011-06-09 11:17 -0700
Re: unions Victor Bazarov <v.bazarov@comcast.invalid> - 2011-06-09 14:29 -0400
Re: unions Edek <edek.pienkowski@gmail.com> - 2011-06-10 13:16 +0200
csiph-web