Topic: reinterpret_cast: cast to refs defined in terms of cast to pointers?


Author: whatiscpp@yahoo.com (John the newbie)
Date: Thu, 30 May 2002 16:25:53 GMT
Raw View
Hi everybody,

a recent answer I had on c.l.c++.m induced me to have a closer look at
the rules governing the reinterpret_cast operator (5.2.10)

In paragraph 10 there's the following text:

"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."

Initially I thought that the part that says "reinterpret_cast<T&>(x)
has the same effect as the conversion *reinterpret_cast<T*>(&x)" was
simply a hint for the reader.
But after meditating a bit, I think instead it says that
reinterpret_cast<T&>(x) IS DEFINED IN TERMS OF (instead of "has the
same effect of") reinterpret_cast<T*>(&x), because otherwise one
cannot apply the definition in 5.2.11p8!

Am I right? Was this the intent?


Another question: doesn't the following invoke undefined behavior when
T is const-qualified?

template <typename T> T* addressof(T& v)
{
  return reinterpret_cast<T*>(
       &const_cast<char&>(reinterpret_cast<const volatile char
&>(v)));
}

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]