Error C2621 when union contains template array in VS2013
I have C ++ code that I am trying to compile in Visual Studio 2013, but I have an error. Here's a simplified test case that demonstrates the problem:
template <typename SomeEnum>
struct Inner {
SomeEnum variant;
int innerVal;
};
template <typename SomeEnum>
struct Outer {
int outerVal;
union {
Inner<SomeEnum> inners[10];
unsigned char data[20];
};
};
enum MyEnum {
VAR1,
VAR2
};
int main() {
Outer<MyEnum> outer;
return 0;
}
This is giving me an error main.cpp(11): error C2621: 'Outer<MyEnum>::inners' : illegal union member; type 'Inner<SomeEnum>' has a copy constructor
. It seems like it Inner<SomeEnum>
should be like PODs as they come. Is this a known issue, or is the code invalid for a reason I am not aware of? Some Googling gave no results on this matter.
The example compiles if I am either Inner
not a template class or inners
an array, but unfortunately neither is an option for my actual code. Are there any other ways that I could accomplish the same thing?
source to share
It works for ideone.com which makes me think it might be a VS2013 bug. You can try VS2015 if you can.
A possible workaround is to be explicitly specialized for each enum you want to use.
Adding this value after definition MyEnum
:
template <>
struct Inner<MyEnum> {
MyEnum variant;
int innerVal;
}
For some reason, the error goes away. Obviously this will lead to a ton of duplicate code trying to stop templates. You could write a macro (s) to do this template specialization for you.
source to share