Topic: Union member and ctor/op=
Author: jkanze@otelo.ibmmail.com
Date: 1998/03/03 Raw View
In article <GEOFFK.98Mar1162421@discus.anu.edu.au>,
geoffk@discus.anu.edu.au (Geoffrey KEATING) wrote:
>
> In article <34F79644.292D@pratique.fr> Valentin Bonnard
<bonnardv@pratique.fr> writes:
>
> > Newsgroups: comp.std.c,comp.std.c++
>
> > union U {
> > int i;
> > float f;
> > T t;
> > };
> >
> > U u1, u2;
> > u1 = u2;
> >
> > Q1 Which member is copied ?
[...]
> so basically, if the last element stored was u2.x, then the assignment
> is equivalent to u1.x = u2.x. Paragraph 6 implies that bytes in u2
> not part of u2.x need not be copied.
>
> In C++, the question makes my head hurt.
It shouldn't. Once you've gone through the list of things you are not
allowed to put into a union in C++, you're back to C.
--
James Kanze +33 (0)1 39 23 84 71 mailto: kanze@gabi-soft.fr
+49 (0)69 66 45 33 10 mailto: jkanze@otelo.ibmmail.com
GABI Software, 22 rue Jacques-Lemercier, 78000 Versailles, France
Conseils en informatique orient e objet --
-- Beratung in objektorientierter Datenverarbeitung
-----== Posted via Deja News, The Leader in Internet Discussion ==-----
http://www.dejanews.com/ Now offering spam-free web-based newsreading
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: jkanze@otelo.ibmmail.com
Date: 1998/03/03 Raw View
In article <34F79644.292D@pratique.fr>,
bonnardv@pratique.fr wrote:
>
> [Note: I'd like to know for both languages, so I cross-post
> this message]
For once, the answer is the same for both langauges.
> Timothy Philip Vernum <tpv01@uow.edu.au> writes:
>
> > A colleague and I were porting some source from MetroWerks CodeWarrio=
r to
> > MS-VC++, and ran into a point where a union had been declared where =
one
of
> > its member classes had an operator=3D (well 2 actually)
>
> I have a related question: why does the operator=3D of an union do ?
A bitwise copy.
In C, all operator=3D's can use bitwise copy. You cannot define it any o=
ther
way.
In C++, the purpose of all of the restrictions as to what may go into a
union is to exclude anything for which a bitwise copy might not work.
> I can't find its definition in the draft.
I'll admit that I haven't looked. The above is from a discussion I had
with Tom Plum and Fergus Henderson concerning the question. And the fact
that there really isn't any other way you could reasonably define it.
> union U {
> int i;
> float f;
> T t;
> };
>
> U u1, u2;
> u1 =3D u2;
>
> Q1 Which member is copied ?
Theoretically, I would guess that it is whichever member the object on
the right hand side held.
> Q2 Does anyone care ?
In practice, no. The restrictions on unions (in C++) are such that the
compiler can use a bitwise copy, and no conforming program can tell the
difference.
> [Q3, if the answer of Q2 is no: Does Jose Lajoie care ?]
Ask her:-). (As the Canadian representative to ISO, her email address
should be in the documentation you have as a member of AFNOR. And BYW:
she's Quebecoise, and and is quite happy to discuss issues in French.)
--
James Kanze +33 (0)1 39 23 84 71 mailto: kanze@gabi-soft.fr
+49 (0)69 66 45 33 10 mailto: jkanze@otelo.ibmmail.com
GABI Software, 22 rue Jacques-Lemercier, 78000 Versailles, France
Conseils en informatique orient=E9e objet --
-- Beratung in objektorientierter Datenverarbeitung
----- Posted via Deja News, The Leader in Internet Discussion
-----
http://www.dejanews.com/ Now offering spam-free web-based newsreading
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Valentin Bonnard <bonnardv@pratique.fr>
Date: 1998/03/01 Raw View
[Note: I'd like to know for both languages, so I cross-post
this message]
Timothy Philip Vernum <tpv01@uow.edu.au> writes:
> A colleague and I were porting some source from MetroWerks CodeWarrior to
> MS-VC++, and ran into a point where a union had been declared where one of
> its member classes had an operator= (well 2 actually)
I have a related question: why does the operator= of an union do ?
I can't find its definition in the draft.
union U {
int i;
float f;
T t;
};
U u1, u2;
u1 = u2;
Q1 Which member is copied ?
Q2 Does anyone care ?
[Q3, if the answer of Q2 is no: Does Jose Lajoie care ?]
--
Valentin Bonnard mailto:bonnardv@pratique.fr
info about C++/a propos du C++: http://www.pratique.fr/~bonnardv/
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: geoffk@discus.anu.edu.au (Geoffrey KEATING)
Date: 1998/03/01 Raw View
In article <34F79644.292D@pratique.fr> Valentin Bonnard <bonnardv@pratique.fr> writes:
> Newsgroups: comp.std.c,comp.std.c++
> union U {
> int i;
> float f;
> T t;
> };
>
> U u1, u2;
> u1 = u2;
>
> Q1 Which member is copied ?
In C, the assignment operator makes the value of u1 the same as the
value of u2. Section 6.1.2.8.1 says
5 When a value is stored in a member of an object of union type, the
bytes of the object representation that do not correspond to that
member take unspecified values, but the value of the union object
shall not thereby become a trap representation.
6 When an operator is applied to a value which has more than one
object representation, which object representation is used shall
not affect the value of the result. When a value is stored in an
object ... it is unspecified which representation is used, ...
so basically, if the last element stored was u2.x, then the assignment
is equivalent to u1.x = u2.x. Paragraph 6 implies that bytes in u2
not part of u2.x need not be copied.
In C++, the question makes my head hurt.
> Q2 Does anyone care ?
Clearly, yes :-).
--
Geoff Keating <Geoff.Keating@anu.edu.au>
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: James Kuyper <kuyper@wizard.net>
Date: 1998/03/02 Raw View
Timothy Philip Vernum wrote:
>
> A colleague and I were porting some source from MetroWerks CodeWarrior to
> MS-VC++, and ran into a point where a union had been declared where one of
> its member classes had an operator= (well 2 actually)
>
> CodeWarrior allowed in but MSVC++ didn't.
> So some flipping through references proved it to be illegal.
>
> Our question is why?
>
> To me it seems reasonable (that it is illegal) but my colleague thinks it
> should be valid.
>
> I feel that it shouldn't be allowed as assignments to the union will result
> in the operator not being called, which is bad.
> He thinks it's ok, since a union is just sharing memory, not objects, and
> whichever memeber you assign to , then thats the correct operator
> ( after all all variables have an operator=, just some are lanaguage
> defined. )
>
> Is there an explanation of why it is illegal anywhere?
A union is not required to store enough information for the compiler to
determine which of its members is currently in use. Therefore, it cannot
automatically determine which member's operator=() to use.
Even if such information were stored, problems remain. Consider:
union T1T2{
T1 a;
T2 b;
} u1, u2;
u1.a = x;
u2.b = y;
u1 = u2;
Should u1.operator=() call u1.a.operator=() or u1.b.operator=()? For
simple classes, u1.b.operator=(u2.b) would work, but not if u1.a is
handling a resource, such as a pointer to allocated memory. Nor would it
work safely if u1.b.operator=() examines the values of any of the
members of u1.b. A workable alternative in some cases would be to use
placement delete and placement new, but that can cause additional
problems.
If you want this functionality, define a structure which contains both
the union and a type identifier of some kind; either an enumeration or a
typeinfo*. Then write an explicit operator=() for that class, and make
it deal with all of the relevant issues. Good luck! Unless you know the
full implementation details of all the members, you're going to need it.
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: tpv01@uow.edu.au (Timothy Philip Vernum)
Date: 1998/02/27 Raw View
A colleague and I were porting some source from MetroWerks CodeWarrior to
MS-VC++, and ran into a point where a union had been declared where one of
its member classes had an operator= (well 2 actually)
CodeWarrior allowed in but MSVC++ didn't.
So some flipping through references proved it to be illegal.
Our question is why?
To me it seems reasonable (that it is illegal) but my colleague thinks it
should be valid.
I feel that it shouldn't be allowed as assignments to the union will result
in the operator not being called, which is bad.
He thinks it's ok, since a union is just sharing memory, not objects, and
whichever memeber you assign to , then thats the correct operator
( after all all variables have an operator=, just some are lanaguage
defined. )
Is there an explanation of why it is illegal anywhere?
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]