On 11/11/08, James Dennett <[EMAIL PROTECTED]> wrote:
> On Tue, Nov 11, 2008 at 8:04 PM, Mark Tall <[EMAIL PROTECTED]> wrote:
> > On 12/11/2008, James Dennett <[EMAIL PROTECTED]> wrote:
> > > In a union only one field can be active at one time, hence
> > > initializing more than one makes no sense
> > > ...
> > > However, const items need to be initialized, hence potting two in a
> > > union makes no sense.
> >
> > Conceptually there is nothing wrong with having two or more const
> > members in a union.
>
> There is something very wrong with it.
>
> The idea of a union is that it's space for one of a collection of
> objects to be active at any one time.  Writing to one member
> invalidates all other members.
>
> (There are secondary uses of unions for type punning.  Most such uses
> are not valid portable C++, but g++ supports them because they're so
> common in real code.)
>
> > The compiler (or standard) should recognise this
> > particular case, and interpret that initialising one member of a union
> > is equivalent to initialising all the members, be they const or not.
>
> The members are only active one at a time.  It makes no sense to speak
> of them all being initialized.
>
> > > (The standard doesn't need an explicit rule to say that, as it's
> > > implied by other rules.)
> >
> > I'm not sure I agree with the standard.  The two requirements, i.e.
> > (i) need to initialise all consts, and (ii) only one member of a union
> > can be initialised, are at conflict with each other. Requirement (ii)
> > fullfills requirement (i), so the compiler shouldn't be complaining
> > about uninitialised consts.
>
> Initializing one member of a union does not (and generally cannot)
> initialize other members.  In general, the purpose of unions is to
> hold objects of different types.  There's no way in which initializing
> (say) a char can also be reasonably viewed as valid initialization of
> (say) a double, or vice versa.  But that's less important: the key
> issue is the concept of a union as having only one active member at
> any given time.
>
> You are, of course, free to dislike this.  C++0x is making substantial
> changes to handling of unions, but I cannot imagine any change such as
> you propose being accepted -- it just does too much violence to the
> model of what unions are.  You can also submit patches for gcc to
> implement what you wish as an extension to the standard; I'd be
> surprised if such a patch were accepted, but I have no way to know for
> sure.

In summary, the change to unions is to allow non-pod types in unions.
To use this capability, though, you need to remember what the last
active field was so you can destruct it.

I agree that having multiple initialized members of a union is highly
unlikely.  The semantics depend too much on the details of compilers
and platforms, so the best the standard could do is "implementation
defined", and the general trend is towards avoiding that were possible.

-- 
Lawrence Crowl

Reply via email to