Consider the following code:
struct A {
int n;
double d;
};
int main() {
auto a = A{.n = 1, .d = 3.14};
// error: static_cast from 'int*' to 'A*' is not allowed
auto _ = static_cast<A*>(&a.n);
}
See https://godbolt.org/z/8814sEh79
However, the documentation of static_cast in cppref says: (emphasis mine)
Two objects a and b are pointer-interconvertible if:
- they are the same object, or
- one is a union object and the other is a non-static data member of that object, or
- one is a standard-layout class object and the other is the first non-static data member of that object or any base class subobject of that object, or
- there exists an object c such that a and c are pointer-interconvertible, and c and b are pointer-interconvertible.
According to the citation above, &a and &a.n should be pointer-interconvertible, so why is static_cast<A*>(&a.n) ill-formed?
static_cast. Note that the link "the C++ standard" references the cppreference site that is a wiki-like site and not the C++ standard.static_cast.static_caststatic_cast, rather thanreinterpret_cast.