According to this and this active member of union doesn't imply any actions by the language/compiler. We must ourselves must make sure that the members are activated/deactivated correctly i.e. by calling their constructor/destructor.
With that in mind, it should be safe to have all members active (its currently working like this, no assignments are made at all), but I wonder if I used undefined behavior at some point (because I definitely don't use the return value of placement new which must be done (or std::launder
) in order to avoid issues through the optimizer)
/// not really a union
/// just a handy way to access the individual elements named
/// and still be able to iterate over them,
/// thus \see bucket and \see buckets are always both active
union Buckets
{
using Type = std::map<int,char>;
struct
{
Type a;
Type b;
Type c;
Type d;
Type e;
Type f;
} bucket;
Type buckets[6];
static_assert(sizeof(bucket) == sizeof(buckets));
Buckets() { for (auto& current : buckets) new(¤t) Type(); }
~Buckets() { for (auto& current : buckets) current.~Type(); }
};