Where in the C++ spec is this allowed? It’s cool. I want to know how this is spec’d. I didn’t realize the spec allowed having a pointer to a undefined type.
class T;
int main(int argc, char* argv[])
{
int x;
T* p = reinterpret_cast<T*>(&x);
return 0;
}
What you’re doing may be legal, but that depends on the real definition of
class T, so we can’t know for sure based on the code you’ve shown.Starting with C++11 §3.9.2/3:
Those restrictions are listed in §3.2/4:
The 6th bullet appears pertinent here, as we can see in §5.2.10/7 that a
reinterpret_castbetween pointer types is defined in terms ofstatic_cast:But because
reinterpret_caststatic_casts tovoid*first, then to the real resulting pointer type, that 6th bullet doesn’t apply.So, so far you’re still in legal territory, despite
Tbeing an incomplete type. However, if it turns out thatTis not a standard-layout type or has stricter alignment requirements thanint, then the last sentence in §5.2.10/7 holds true and you’re invoking UB.