If A
now is replaced by std::pair<T1, T2>
, will this work for every possible T1
, T2
too?
No, it wouldn't even work in this specific case.
would this affect the possibility to safely reinterpret_cast
in this case?
Not at all. The cast is invalid in both cases.
You are violating the informal strict aliasing rule. More specifically, according to the standard, you are casting a pointer to an object (here &a
, where a
is the object) to B*
. Any attempt to dereference that B*
will be UB because you do not have an object B
.
You might think that you have an object, but actually, according to the standard, you don't. From [intro.object]p1:
An object is created by a definition ([basic.def]), by a new-expression, when implicitly changing the active member of a union ([class.union]), or when a temporary object is created ([conv.rval], [class.temporary]).
None of those cases apply, so you have UB if you try to use the "object" b
which isn't one.
Now, you should consult with your implementation, because a lot of implementations provide a basic guarantee for constructs like yours IIRC.