Your union is anonymous. Thus, the compiler will create arr1
and arr2
at class-level.
Because of this, char arr2[sizeof(arr1)];
won’t refer to arr1
properly.
Here is a workaround :
template <size_t N>
struct S
{
union A
{
int arr1[N];
char arr2[sizeof(arr1)];
};
};
Compiles fine here : https://ideone.com/JcvOYg
By naming the union, we prevent the compiler from including it directly. It is then able to retrieve arr1
properly.
But it also means that arr1
and arr2
aren’t not S
‘s members anymore.
Finally, Members can be defined in terms of other members
but the latter has to be “findable” by the compiler.