|
Boost : |
From: Douglas Gregor (gregod_at_[hidden])
Date: 2002-11-15 16:13:42
On Friday 15 November 2002 03:45 pm, Douglas Gregor wrote:
> On Friday 15 November 2002 03:36 pm, Gennaro Prota wrote:
> > of inventing a separate concept (say "Addressable") for them. The problem
> > I see in your resolution is that AFAIK addressof() is not guaranteed to
> > work by the standard. Yes, it has an extremely high probability to do the
> > right thing, but not a guarantee.
>
> You'll have to back that up with some standardese. AFAICT, 5.2.10/10 lets
> addressof() work:
> "That is, a reference cast reinterpret_cast<T&>(x) has the same effect as
> the conversion *reinterpret_cast<T*>(&x) with the builtin & and *
> operators."
>
> (And that reinterpret casting T* -> U* -> T* preserves the original value).
>
> Doug
Just to spell it out in agonizing detail, so we can look for any holes in my
logic:
template <typename T> T* addressof(T& v)
{
return reinterpret_cast<T*>(
&const_cast<char&>(reinterpret_cast<const volatile char &>(v)));
}
By 5.2.10/10, "reinterpret_cast<const volatile char &>(v)" is equivalent to
"*reinterpret_cast<const volatile char *>(&v)" with the built-in operators &
and *.
Substitute this into the original expression:
reinterpret_cast<T*>(
&const_cast<char&>(&*reinterpret_cast<const volatile char *>(&v)));
The const/volatile qualifiers don't matter at all in reinterpret_cast or
const_cast and the object is guaranteed to be the same object through the
const_cast (5.2.10/7 and 5.2.11/3, respectively). Now we just have the case
where we're reinterpret_casting T*->char*->T*, and that's covered by
5.2.10/7.
Doug
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk