I
think that the point of the article (example 1 vs example 3) is that you are
not allowed to cast or unionize pointers. You must store and fetch
from a location that’s declared as a union.
Now
what if you cast, not from T1* to T2*, but from T1* to U*? Does the union
itself have to be defined as a union, or can you cast the original typed
pointer to the union pointer, perhaps as the parameter to a function call so
that only the U* pointer is in scope and being dereferenced while T1* in the
caller is dormant and assumed to be “changed” by virtue of the
call.
Perhaps
a close reading of the spec will indicate that a “barrier” is
possible where two pointers are not used at the same time. I’m
certain that works in practice though: upon calling foo(p);, the compiler
assumes that *p has been changed. Within the body of foo, p is not used
at all but q is used, which has the same address and different type. If p
is not used, you don’t have to worry about it being assumed not to change
just because q is used. However, inlining complicates things.
I
suppose to be ratified, a portable solution is needed. But individual
compiler-specific solutions would implement the alias_cast by saying “yes
this will alias” in a compiler-specific manner. The general
solution is only for formality and bootstrapping a port to a different
compiler/version, so it doesn’t need to be that efficient.
From:
boost-users-bounces@lists.boost.org [mailto:boost-users-bounces@lists.boost.org]
On Behalf Of Neil Groves
Sent: Monday, May 24, 2010 1:14 PM
To: boost-users@lists.boost.org
Subject: Re: [Boost-users] [Review] Formal Review: Boost.Move
On Mon, May 24, 2010 at 6:59 PM, John Dlugosz <JDlugosz@tradestation.com>
wrote:
>
> The problem is not downcasting, but the fact that T is not rv<T>
(the
> dynamic type of T is T, not rv<T>):
>
Reading the referenced article
on C99 aliasing rules, perhaps what is needed is a alias_cast template that
does it right (e.g. pass it through a union or use proper compiler-specific
decorations).
That was my first thought too, but as far as I know all
correct uses of a union would mandate a fully copy of the T instance. I think
that the containment by pointer that was suggested by Steven Watanabe makes for
a nice solution.
If you have a novel solution that uses a union while
avoiding assignment to a new T instance I would be very interested.
Regards,
Neil Groves