I believe that the confusion comes from the fact that the C language explicitly permits type-punning through a union, whereas C++ (C++11) has no such permission and many of us (old-timers) have learnt C first (C++ was not a thing when I started...) and old habits die hard...
The compiler we use is a C++ compiler, so as discussed, the union was guiding you in the uncharted territory of "undefined behavior" (UB) of the C++ language.
I'm sure that would have worked, because the compiler tries to stay compatible with C style code when it can, but it's not recommended to depend on a compiler's specific, case dependant, implementation. Better stick with the standard.