Re: Anonymous union rules against constructors & assignment
<kuyper@wizard.net> wrote in message
news:1149782827.305720.255770@i39g2000cwa.googlegroups.com...
Crosbie Fitch wrote:
But you said it would have the same effects as the corresponding struct
you showed, which does involve default constructor calls. I think you
might want to come up with some way of describing what you want to have
happen, that doesn't involve that comparison with a struct type.
Please forgive my uncompilable and incomplete examples.
Disregard the other member, thus:
Apart from overlaid storage there should be no difference between:
{ struct S { B b; } s; s.b=B(123); }
and
{ union U { B b; } u; new(&u.b) B(); u.b=B(123); u.b.B::~B(); }
Given:
struct B
{ int a;
B():a(0) { }
B(int i):a(i) {}
B(const B& b):a(b.a) { }
~B() { }
B& operator=(const B& b) { a=b.a; return *this; }
}
I'm saying that if you allow non-POD types in your union, they either
run the risk of not having their constructors called, or of having
constructors called for overlapping objects without an intervening
destructor call.
Not if the compiler butts out the moment a non-POD member is present in the
union.
It won't call any constructors and will refuse to provide a default
constructor for the enclosing class.
One of the key advantages of C++ over C is the
guarantees it provides about automatic construction and destruction of
objects, and I don't think it's a good idea to extend C unions to allow
members to have types for which those guarantees are both desireable
and impossible to provide.
unions should be thought of as overlays, not objects. Thinking of a union as
an object leads you astray - into saying it's an object only as long as what
it contains can be treated as not an object. That's sophistry.
And for non-POD types, that violates the design of C++.
No it doesn't. If you can't guarantee construction of non-POD members, don't
provide a default constructor in that event. That's better than simply
prohibiting non-POD members. NB I'm only really after this for anonymous
unions anyway.
Any object with
a non-trivial constructor should have that constructor called.
You're right.
And that's why, when a union has a member with a non-trivial constructor (a
non-POD class) that union cannot have a default constructor created for it.
Instead, the programmer must do it explicitly, in-place.
However, a union isn't an object, but an overlay - if it has non-POD members
it always needs non-trivial construction - you can't leave the compiler to
create a default constructor for a union that has objects with non-trivial
constructors.
Using placement new to invoke the constructor is a hack. It's a necessary
hack, and I wouldn't recommend that it be dropped from the language,
but every time you use it you should be asking yourself whether there's
any other way to do what you want; if the answer is yes, it's probably
a better way of doing that task. Allowing non-POD types in unions only
becomes workable by use of placement new, or equivalent mechanisms.
Placement new is just as esoteric as using an anonymous union in the first
place. If you're using an anonymous union with non-POD members it's as
esoteric as writing your own memory allocator anyway.
Could you explain that chain of inference?
I could guess at an explanation, but I doubt it would be particularly
accurate.
---
[ 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 ]