In the current draft standard (and C++17), this is written about static_casting a void *
:
A prvalue of type “pointer to cv1 void” can be converted to a prvalue of type “pointer to cv2 T”, where T is an object type and cv2 is the same cv-qualification as, or greater cv-qualification than, cv1. If the original pointer value represents the address A of a byte in memory and A does not satisfy the alignment requirement of T, then the resulting pointer value is unspecified. Otherwise, if the original pointer value points to an object a, and there is an object b of type T (ignoring cv-qualification) that is pointer-interconvertible with a, the result is a pointer to b. Otherwise, the pointer value is unchanged by the conversion.
I wonder, what is the difference whether the conversion is pointer-interconvertible or not? Is there a case, when casting a void *
to something pointer-interconvertible actually changes the pointer value? What is the intent of this distinction?
For completeness pointer interconvertible:
Two objects a and b are pointer-interconvertible if:
- (4.1) they are the same object, or
- (4.2) one is a union object and the other is a non-static data member of that object ([class.union]), or
- (4.3) one is a standard-layout class object and the other is the first non-static data member of that object, or, if the object has no non-static data members, any base class subobject of that object ([class.mem]), or
- (4.4) there exists an object c such that a and c are pointer-interconvertible, and c and b are pointer-interconvertible.
If two objects are pointer-interconvertible, then they have the same address, and it is possible to obtain a pointer to one from a pointer to the other via a reinterpret_cast.