| View previous topic :: View next topic |
| Author |
Message |
mdlinux7@yahoo.co.in Guest
|
Posted: Wed Oct 19, 2005 8:42 am Post subject: Unions to avoid strong C++ type checking |
|
|
Hi All,
Can anyone give me a pratical examples where unions can be used to
avoid C++ type checking.
Regards
Dinesh
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Dan McLeran Guest
|
|
| Back to top |
|
 |
Allan W Guest
|
Posted: Thu Oct 20, 2005 11:24 am Post subject: Re: Unions to avoid strong C++ type checking |
|
|
Dinesh (mdlinux7 (AT) yahoo (DOT) co.in) wrote:
| Quote: | Can anyone give me a pratical examples where unions can be used to
avoid C++ type checking.
|
I hope not.
If you're trying to do this for one or two special cases, such
as a database field, you should probably look at one of the
classes in Boost, such as boost::any or boost::variant.
See http://boost.org , http://boost.org/doc/html/any.html , and
http://boost.org/doc/html/variant.html .
If you're just trying to avoid specifying the types of your
data fields -- C++ isn't the best language for that. C++
type checking exists for a reason, and if you fight it, you're
going to get a program that's big and slow and difficult to
maintain.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Gabriel Dos Reis Guest
|
Posted: Thu Oct 20, 2005 11:27 am Post subject: Re: Unions to avoid strong C++ type checking |
|
|
[email]mdlinux7 (AT) yahoo (DOT) co.in[/email] writes:
| Quote: | Hi All,
Can anyone give me a pratical examples where unions can be used to
avoid C++ type checking.
|
I'm interested in why you might want to do so.
--
Gabriel Dos Reis
[email]gdr (AT) integrable-solutions (DOT) net[/email]
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Carlos Moreno Guest
|
Posted: Thu Oct 20, 2005 12:26 pm Post subject: Re: Unions to avoid strong C++ type checking |
|
|
[email]mdlinux7 (AT) yahoo (DOT) co.in[/email] wrote:
| Quote: | Hi All,
Can anyone give me a pratical examples where unions can be used to
avoid C++ type checking.
|
I would actually be more curious about seeing an example where one
would legitimately *want to avoid* C++ type checking...
Carlos
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
kanze Guest
|
Posted: Fri Oct 21, 2005 4:15 pm Post subject: Re: Unions to avoid strong C++ type checking |
|
|
Gabriel Dos Reis wrote:
| Quote: | mdlinux7 (AT) yahoo (DOT) co.in writes:
| Can anyone give me a pratical examples where unions can be
| used to avoid C++ type checking.
I'm interested in why you might want to do so.
|
I can think of at least two cases where I did so in C: in the
implementation of malloc/free/realloc in a C library, and in the
implementation of ldexp/frexp/modf in the same library.
Formally, my use was undefined behavior, according to the C
standard. However 1) I wrote the code before there was a C
standard, and I believe that Kernighan and Richie intended for
unions to be usable this way, and 2) the code was anything but
portable anyway, so I felt safe counting on my compiler
guarantees. Still, today I'd use pointer casts to do the same
thing. But in C++ terms, they'd be reinterpret_cast's, and not
really any more portable than the union trick.
Of course, this is very low level code. Not the sort of thing
you'd be doing in a typical application.
--
James Kanze GABI Software
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Gabriel Dos Reis Guest
|
Posted: Sat Oct 22, 2005 3:21 pm Post subject: Re: Unions to avoid strong C++ type checking |
|
|
"kanze" <kanze (AT) gabi-soft (DOT) fr> writes:
| Quote: | Gabriel Dos Reis wrote:
[email]mdlinux7 (AT) yahoo (DOT) co.in[/email] writes:
| Can anyone give me a pratical examples where unions can be
| used to avoid C++ type checking.
I'm interested in why you might want to do so.
I can think of at least two cases where I did so in C: in the
|
I was hoping "mdlinux7 (AT) yahoo (DOT) co.in" would clarify what he want to
achieve with nontypeful programming in C++.
--
Gabriel Dos Reis
[email]gdr (AT) integrable-solutions (DOT) net[/email]
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
|