From -9070934885698482407
X-Google-Thread: f78e5,1130823ffefc6ebe
X-Google-Attributes: gidf78e5,public
X-Google-Language: ENGLISH,ASCII
Path: g2news1.google.com!news1.google.com!news.glorb.com!border1.nntp.dca.giganews.com!nntp.giganews.com!peer01.cox.net!cox.net!peer-uk.news.demon.net!kibo.news.demon.net!mutlu.news.demon.net!news.demon.co.uk!demon!stump.algebra.com!devnull
From: swelef@post.sk (Vladimir Marko)
Newsgroups: comp.std.c++
Subject: Re: Unions up in arms!
Date: Fri, 17 Sep 2004 06:14:59 GMT
Organization: http://groups.google.com
Lines: 66
Sender: mail2news@demon.net
Approved: fjh@cs.mu.oz.au (Fergus Henderson , moderator of comp.std.c++)
Message-ID: <84523212.0409160407.d34ab40@posting.google.com>
References: <9215d7ac.0409081832.4ab088d6@posting.google.com> <d6652001.0409130421.2c2cccc1@posting.google.com> <84523212.0409140907.46471b77@posting.google.com> <41476579$0$12975$626a14ce@news.free.fr>
NNTP-Posting-Host: news.news.demon.net
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: 8bit
X-Trace: news.demon.co.uk 1095401703 14830 158.152.254.254 (17 Sep 2004 06:15:03 GMT)
X-Complaints-To: abuse@demon.net
NNTP-Posting-Date: Fri, 17 Sep 2004 06:15:03 +0000 (UTC)
X-Robomod: STUMP, ichudov@algebra.com (Igor Chudov)
X-Spam-Checker-Version: SpamAssassin 2.64-mulga_r1 (2004-01-11) on 
	mulga.cs.mu.OZ.AU
X-Spam-Status: No, hits=-4.9 required=5.0 tests=BAYES_00 autolearn=ham 
	version=2.64-mulga_r1
X-Received: (from fjh@localhost)
	by mulga.cs.mu.OZ.AU (8.12.10+Sun/8.12.9/Submit) id i8H6ExKN006050;
	Fri, 17 Sep 2004 16:14:59 +1000 (EST)
X-Path: comp-std-cpp-robomod!not-for-mail
X-NNTP-Posting-Date: Thu, 16 Sep 2004 12:07:22 +0000 (UTC)
X-Delivered-To: std-c++@ucar.edu
X-Authentication-Warning: mulga.cs.mu.OZ.AU: fjh set sender to devnull@stump.algebra.com using -f
X-Newsgroups: comp.std.c++
Xref: g2news1.google.com comp.std.c++:2684

tannhauser86549spam@free.fr (Falk Tannh�user) wrote in message news:<41476579$0$12975$626a14ce@news.free.fr>...
> Vladimir Marko wrote:
> 
> > kanze@gabi-soft.fr wrote in message news:<d6652001.0409130421.2c2cccc1@
>  posting.google.com>...
> > 
> >>    struct S { int i ; float f ; } ;
> >>    float ff ;
> >>
> >>    S* ps ;
> >>    float* pf ;
>  [...]
> >>For the rest, there are a lot of other rules which have to be taken
> >>into account.  Thus, for example, *(float*)ps is definitly not
> >>a legal access, despite the text you quote.
> > 
> > Oh, really? First, we have 9.2/17:
> >   A pointer to a POD-struct object, suitably converted using a
> >   reinterpret cast, points to its initial member (or if that member
> >   is a bit-field, then to the unit in which it resides) and vice versa.
> > Thus, *(int*)ps is surely valid.
> 
> OK. It's an lvalue referring to the same object as 'ps->i'.
> 
> > Then, 9.5/1 says
> >   [snip] Each data member is allocated as if it were the sole member of
> >   a struct. [snip]
> 
> But 'ps' in the above example is a pointer to struct, and   9.5/1
> concerns unions.

I appologize. The thread is about unions and I misread the Kanze's
example as if there was a union too. (And I also forgot to mention
that 9.5 is about unions.)

> If 'S' were a union, I agree that 'reinterpret cast<float*>(ps)'
> would be well defined and equal to '&ps->f'.
> 
> > It comes down to the question "How deep is that 'as if'?" Is it deep
> > enough for the 9.2/17 to apply?
> 
> Well, being "the sole member of a struct" implies being its initial
> member, so   9.2/17 indirectly applies to each union member IMO.

The wording is "allocated as if". This suggests that it only applies
to the memory layout and 9.2/17 is about reinterpret_cast validity
not about memory layout. It definitely can't apply to the paragraph
8.5.1/15 [dlc.init.aggr/unions]:
  When a union is initialized with a brace-enclosed initializer,
  the braces shall only contain an initializer for the first member
  of the union.
And what's the difference between "initial member" (in 9.2/17 and
"first member" (in 8.5.1/15)?

I'll just repeat myself by writing that I think that the standard
says "undefined behaviour" while each reasonable implementation
makes it well defined.

Vladimir Marko

---
[ 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.jamesd.demon.co.uk/csc/faq.html                       ]



