Re: Anonymous union rules against constructors & assignment
<johnchx2@yahoo.com> wrote in message
news:1149552168.378163.198510@h76g2000cwa.googlegroups.com...
I see...I hadn't realized that you were proposing a wholesale rewrite
of the semantics of unions. Basically, the proposal seems to boil down
to "treat all members of unions as if they were POD whether they are
POD or not."
Not at all.
Treat all members of unions like members of any other class, recognised
precisely for what they are.
Apart from overlaid storage there should be no difference between:
struct S { A a; B b; } s; s.b=B(123);
and
union U { A a; B b; } u; new(&u.b) B(); u.b=B(123);
and no restrictions on the members of A or B,
EXCEPT that:
The members of U are not initialised, copied or destroyed by default (unless
the language provides a way to override that default). The union is
considered independently from its members, so the union is copied bitwise by
default (potentially overriden come the glorious day when unions are
upgraded to class status).
It boils down to the union not being treated like a struct. Rather than try
and shoehorn the C concept of a union into some whacky OO doppelganger
device (which has not been achieved), leave it as it was in C, i.e. overlaid
memory. The worst thing to do is to blunt the sharp edges off and make it a
vestigal C curiousity for beginners and almost useless for experts.
If you really wanted to be symmetrical about it you should probably have a
way of explicitly requesting that members of a class not be initialised or
destroyed by default, e.g. S():~a,b(123) { }. At least you can already
override their default copying.
You'd also need to use casts to access the
members as constructed objects, but that's just telling the truth
again, and you don't seem to mind casts.
I do indeed mind casts. I've used them only to show how a union becomes a
far
more convenient equivalent.
The whole point of unions (like structs) is a syntactical
convenience. Otherwise you might as well use mallocs, pointers and casts
throughout.
Remember, I'm not asking for the language to be extended. I'm asking for the
hobbles to be removed, i.e. the unnecessary restrictions on union members.
unions are copied by memcpy anyway. To imagine that actually, each member's
assignment-copy operator is called is a delusion invented in the name of
semantic purity by those who could conceive of little use for a union in C++
and regarded it as a dangerous anachronism to be deprecated.
Those who want a safe language should use C#. I see no reason why sharp
knives should be removed from C++ or blunted just because they present a
risk to beginners. Pointers are a risk. Casts are a risk. Unions are a risk.
By all means provide warnings (as for assignments within expressions), but
for god's sake please permit their unadulterated use.
---
[ 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://www.comeaucomputing.com/csc/faq.html ]