Subject: Re: toolchain/22118: make won't compile with -Wcast-qual
To: Greywolf <firstname.lastname@example.org>
From: Richard Earnshaw <email@example.com>
Date: 07/16/2003 11:30:24
> Thus spake Greg A. Woods ("GAW> ") sometime Today...
> GAW> [ On Tuesday, July 15, 2003 at 20:01:46 (+0200), Matthias Drochner wrote: ]
> GAW> > In cases like the iov I'd probably prefer a union of a const and a non-const.
> GAW> That's called pointer aliasing. It's not portable nor safe, and it
> GAW> disables the "const" warnings behind the compiler's back.
It is not pointer aliasing. Read the standard (specifically section 6.3).
foo and const foo are in the same alias set (one is a qualified version
of the other).
> How is that not portable? Are there really implementations that use
> non-overlapping unions?
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" 188.8.131.52).
I guess it would be possible to design a system where, say, the top bit of
an address represented the const-ness of an object, and where any attempt
to write via a pointer with the top bit set would cause a fault; pointers
that differed only in their top bit would access the same memory location.
Applying casts (even implicitly) would strip or add (normally add) the
bit as appropriate. The union trick would fail in those circumstances
since you would be bypassing the cast operations.
C++ has const_cast for these circumstances (explicitly and safely casting
away const-ness). Unfortunately, C has no direct equivalent.