On 2017/4/8 1:00, David Grayson wrote: > I did read the top two answers in the link that Norbert posted: > > http://stackoverflow.com/questions/11373203/accessing-inactive-union-member-and-undefined-behavior > > The first answer (from ecatmur) indicates that this kind of conversion with > a union would be undefined behavior in C++, but not C. I'm not sure what > else to read, except the latest C++ standard, which was quoted heavily in > the answer. That is not exactly the case here.
At the moment `const T *` and `T *` are not layout-compatible types ([basic.types]/11). However, they shall have the same value representation and alignment requirements ([basic.compound]/3). If they were layout-compatible, it would be perfectly defined to write one and read the other, because both are part of the common initial sequence ([class.mem]/22, 23). After all, we generally don't care about this problem because there is a huge amount of code that relies on such UB (`LARGE_INTEGER` for example, and `sockaddr`, `sockaddr_in`, `sockaddr_in6` etc). There is no reason for a compiler to break such code. -- Best regards, LH_Mouse ------------------------------------------------------------------------------ Check out the vibrant tech community on one of the world's most engaging tech sites, Slashdot.org! http://sdm.link/slashdot _______________________________________________ Mingw-w64-public mailing list Mingw-w64-public@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/mingw-w64-public