Subject: Re: toolchain/22118: make won't compile with -Wcast-qual
To: None <tech-toolchain@NetBSD.org>
From: der Mouse <mouse@Rodents.Montreal.QC.CA>
List: tech-toolchain
Date: 07/16/2003 21:28:41
> It's not portable in that the standard says that a union may only
> contain one of its members at a time.  The standard does require that
> they be in overlapping memory, though it doesn't go out and
> explicitly say so (it's a consequence of another requirement: "A
> pointer to a union object, suitably converted, points to each of its
> members" 6.5.2.1).

That doesn't require overlapping memory, except for identical (or
sufficiently close to identical) types.  Given

union u { int i; double d; };

there's no reason that converting union u * to int * or to double *
shouldn't involve, loosely speaking, adding an appropriate constant.
(Well, there's a practical reason, in that there's a comparative lot of
code that assumes that unions _do_ use overlapping memory.  But that's
not the sort of thing we're talking about here.)

Now, for

union v { char *a; char *b; };

then a and b _do_ have to use the same memory, because converting a
union v * to a char ** must result in a pointer to either a or b.
Whether the like applies to union w { char *a; const char *b; }; is a
question I will leave up to better language lawyers than I.

/~\ The ASCII				der Mouse
\ / Ribbon Campaign
 X  Against HTML	       mouse@rodents.montreal.qc.ca
/ \ Email!	     7D C8 61 52 5D E7 2D 39  4E F1 31 3E E8 B3 27 4B