Union sometimes useful in cases, when different types of data can be stored within same data structure. For example, sometimes it is convenient to have different "view" of same data, like integers as series of bytes and vice versa. In practice it's rarely necessary, quite low level and often error prone. There is an type safe alternative to unions - std:variant.
Union sometimes useful in cases, when different types of data can be stored within same data structure. For example, sometimes it is convenient to have different "view" of same data, like integers as series of bytes and vice versa. In practice it's rarely necessary, quite low level and often error prone. There is an type safe alternative to unions - std:variant.
Ah, that makes sense - I've been using std::variant. Seems like unions are most useful for interior with C that uses them.
As of C++17, you should use
std::variant
instead of plain old unions :)