C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Unions to avoid strong C++ type checking

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
mdlinux7@yahoo.co.in
Guest





PostPosted: Wed Oct 19, 2005 8:42 am    Post subject: Unions to avoid strong C++ type checking Reply with quote



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





PostPosted: Thu Oct 20, 2005 11:10 am    Post subject: Re: Unions to avoid strong C++ type checking Reply with quote



Here's an example:

http://www.cuj.com/documents/s=9360/cujexp0410wilson/


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Allan W
Guest





PostPosted: Thu Oct 20, 2005 11:24 am    Post subject: Re: Unions to avoid strong C++ type checking Reply with quote



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





PostPosted: Thu Oct 20, 2005 11:27 am    Post subject: Re: Unions to avoid strong C++ type checking Reply with quote

[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





PostPosted: Thu Oct 20, 2005 12:26 pm    Post subject: Re: Unions to avoid strong C++ type checking Reply with quote

[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





PostPosted: Fri Oct 21, 2005 4:15 pm    Post subject: Re: Unions to avoid strong C++ type checking Reply with quote

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





PostPosted: Sat Oct 22, 2005 3:21 pm    Post subject: Re: Unions to avoid strong C++ type checking Reply with quote

"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
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.