I was wondering if the following was valid C++:
union id {
struct {
std::uint32_t generation : 8;
std::uint32_t index : 24;
};
std::uint32_t value;
};
I want this so I can access both generation
and index
separately, which keeping access to the whole number. Since they all are std::uint32_t
, this shouldn't be UB right?
I plan to use it like that:
auto my_id = id{
.generation = 1,
.index = 4,
};
auto my_id_value = std::uint32_t{id.value};
If it is UB, is there another way to make this work and valid according to the C++ standard?