Two arrays in a union in C++-Collection of common programming errors

Both arrays are required to have the same size and layout. Of course, if you initialize anything using m_V, then all accesses to m_M are undefined behavior; a compiler might, for example, note that nothing in m_V has changed, and return an earlier value, even though you’ve modifed the element through m_M. I’ve actually used a compiler which did so, in the distant past. I would avoid accesses where the union isn’t visible, say by passing a reference to m_V and a reference to m_M to the same function.

Originally posted 2013-11-09 22:49:58.