From: Douglas Gregor (gregod_at_[hidden])
Date: 2002-11-20 10:00:14


On Saturday 16 November 2002 12:24 pm, Gennaro Prota wrote:
> Sorry for the late reply (it's just my timezone).
>
> You wrote:
> >I don't see the contradiction here. 5.2.10/7 says that you can cast from a
> > T pointer to a U pointer and back to a T pointer and get the original
> > pointer back.
>
> Unfortunately the standard is a great piece of work but fails
> miserably to support you when you want to "deduce" things that are not
> written explicitly there in plain English, especially when generic
> expressions like "it's the same as", "is equivalent to", etc. are
> used.

I would generally agree that such an informal style doesn't allow any
deduction, but I think we have to be realistic. We don't have a formal
specification for C++, and it's unlikely that we will ever have one. We won't
get anywhere if we require formal methods with an informal specification.

> First of all the quotes:
>
> 5.2.10/7: "A pointer to an object can be explicitly converted
> to a pointer to an object of different type.65) Except that
> converting an rvalue of type "pointer to T1" to the type
> "pointer to T2" (where T1 and T2 are object types and where
> the alignment requirements of T2 are no stricter than those
> of T1) and back to its original type yields the original pointer
> value, the result of such a pointer conversion is unspecified."
>
>
> Incidentally, what does "converting back" means? However that's not
> the main point.

Given T* tp, reinterpret_cast<T*>(reinterpret_cast<U*>(tp)) is semantically
equivalent to tp.

>
> 5.2.10/10: An lvalue expression of type T1 can be cast to the type
> "reference to T2" if an expression of type "pointer to T1" can be
> explicitly converted to the type "pointer to T2" using a
> reinterpret_cast. That is, a reference cast reinterpret_cast<T&>(x)
> has the same effect as the conversion *reinterpret_cast<T*>(&x)
> with the built-in & and * operators.
>
>
> What does it mean "has the same effect of"?

Would you prefer "is semantically equivalent to?" You can rewrite one
expression as the other. (Not in actual C++ code, because you can't say "I
want the built-in operator&", but it's fine for exploring the properties of
an expression).

> 5.2.10/7 says that the
> result of reinterpret_cast<T*>(&x) is unspecified. What is the effect
> of dereferencing it?
>
> *reinterpret_cast<T*>(&x)

The effect is the same as dereferencing any pointer. You get the lvalue
associated with the address stored in the pointer.

        Doug