Re: Casting void*, comparing void*
On 2015/02/04 15:51, Jens M??ller wrote:
Given the following example code:
class A {
public: A() {}
};
class B {
public; B() {}
};
A* a = new A();
B* b = new B();
void *aVoid = a;
void *bVoid = b;
bool compareVoid = (aVoid == bVoid); // (1) false?
B* aCastedToB = static_cast<B*> aVoid;
bool compareB = (aCastedToB == b); // (2) false or unspecified?
Unfortunately, I only have access to ISO/IEC 14882:2003 here. Maybe
someone can confirm whether my interpretation of the standard held at
that time, and, more importantly, whether it still holds for the current
version of the standard.
In the current draft [1], 3.9.2:3 [basic.compound] says that void*
should, for any type T, be able to hold T*, which is a memory location
of the object. So one can use void* to check whether objects are the
same, regardless of type.
Then 5.2.9:13 [expr.static.cast] says that one can static_cast void* to
any T*: nullptr is always preserved in these casts, but otherwise only
guaranteed to be preserved for the same type - the result of other
conversions is unspecified.
Pointer values can change in the case of multiple inheritance, dependent
on the compiler. If A derives from both B1 and B2, and in the
implementation the latter is the second block, then a cast from A to B2
may change the pointer value; Clang does this if the size of B1 is
non-zero. So casting from A* to void* and then to B2* gives the A*
value, whereas casting directly from A* to B2* gives the pointer of the
B2 subobject.
1. https://github.com/cplusplus/draft
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]