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. The second answer (from Bo Persson) provides a useful link to the GCC documentation which makes me think that GCC provides additional guarantees beyond what the standard says, and so this kind of conversion would actually be safe, in both C and C++. Since GCC behaves that way, clang probably would too. So yeah, the union conversion is probably fine because of the design of the compilers we care about. I think casting would work too. When LH_Mouse's reasoned about why the warnings would not be a problem, Kai said: "Each compiler has its own variants for this. But well, why we should rely on such things." Are you more OK with relying on the details of compiler behavior for union conversions than the details of compiler behavior for warnings? --David On Fri, Apr 7, 2017 at 9:31 AM, Kai Tietz <[email protected]> wrote: > 2017-04-07 17:21 GMT+02:00 David Grayson <[email protected]>: > >> type1 *foo(const type1 *my_const_ptr) > >> { > >> union { > >> type1 *t1; > >> const type1 *ct1; > >> } v; > >> v.ct1 = my_const_ptr; > >> return v.t1; > >> } > > > > Yes, that is sad, and it seems like just a matter of time before a C++ > > compiler looks at the code above and reasons that the write to ct1 can be > > optimized away because there is no code that ever reads from ct1. Thus > the > > read from t1 will be recognized as undefined behavior (probably called a > > poison value in LLVM) and anything could happen. > > LOL ... if a compiler modifies a technical valid language construct, > and produces by this optimization something it needs to warn about, > then the compiler has a bug. And well, you should be sad to use such > a compiler, as it is broken in many aspects. > > > > What was the problem with Mateusz's patch? I think people are implying > it > > results in compiler warnings, but I thought casting a pointer to a > pointer > > usually doesn't give any warnings. > > The problem is that you seems to see an UB, where actually none is. > Please try read a bit about C++, compatibility to C, and what, and how > a union on pointer types are treated in a union (especially the part > about integer scalars & pointers). > > ~Kai > > PS: As you already recognized, yes the issue is to avoid warnings OP > doesn't expect them. > > ------------------------------------------------------------ > ------------------ > 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 > [email protected] > https://lists.sourceforge.net/lists/listinfo/mingw-w64-public > ------------------------------------------------------------------------------ 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 [email protected] https://lists.sourceforge.net/lists/listinfo/mingw-w64-public
