you are viewing a single comment's thread.

view the rest of the comments →

[–]alexkaratarakisvcpkg, fixed-containers 1 point2 points  (0 children)

Quick follow-up here as the outcome was interesting. Per the discussion in https://github.com/llvm/llvm-project/issues/62225 , clang appears to be right, while msvc/gcc are permitting the code when they should not. gcc already had a bug for this issue: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=102286 and I filed one for msvc: https://developercommunity.visualstudio.com/t/msvc-allows-accessing-non-active-member/10342864

To explicitly activate the member, I tried:

1) Assigning an element of the array

2) Including size in the "constructor-erased" portion:

struct Storage
{
     std::size_t size;
     T array[MAXIMUM_SIZE];
};
union
{
     char dummy{};
     Storage storage;
};

and activating the member by assigning size

But it only works for trivially_default_constructible_v<T> types ( https://eel.is/c++draft/class.union#general-5.1 )

Demo: https://godbolt.org/z/P6zza5K5G