Skip to main content
Journey Uncommon Logo
JourneyUncommon
hardC++strict aliasing rulesSingle-choice MCQ

Under the C++ strict aliasing rules, which one of these accesses is GUARANTEED well-defined (not UB) purely on aliasing grounds, assuming the pointed-to bytes already hold a valid object of the original type?

// Assume each pointer points at a live object of its declared underlying type. long l; auto a = *reinterpret_cast<unsigned long*>(&l); // (A) float f; auto b = *reinterpret_cast<int*>(&f); // (B) struct S{int i;}; S s; auto c = *reinterpret_cast<int*>(&s); // (C) int i; auto d = *reinterpret_cast<short*>(&i); // (D)