Recently, I try to convert my VS2008 c++ project to VS2019, because need to update libray, support etc, I have snippet, compiled on VS2008 but not with vs2019
struct A
{
WORD insideA;
A(): insideA(0)
{}
~A(){}
}
struct B
{
WORD insideB;
B():insideB(0)
{}
~B(){}
}
struct AB
{
union
{
struct { A dataA; };
struct
{
WORD wX;
B dataB;
}
}
AB()
:wX(0)
{}
}
struct USAGE
{
AB usageAB;
USAGE(AB &parAB)
: usageAB(parAB) //<< attempting to reference a deleted function
{}
}
Is there any standard changes with anonymous union between vs2008 and vs2019?
Your code was missing some semicolons here and there, and the union was missing member names. I've made a few changes of which I hope that the code still reflects your case. Yes, there were standard changes with respect to unions: before C++11, union member with non-trivial constructors were not allowed. From 11 onwards they are, but then the union must also provide a constructor. So, I guess that your code did compile under vs2008 due to a non-standard language extension of MS.
What you need, to get your code going, is a destructor which calls the appropriate destructor of the constructed union member. Then you must know, which member was constructed. I've solved that below by adding an
m_which
member ofenum class which_t
.Another solution is getting rid of the destructors
~A()
and~B()
.You may also consider using
std::variant
, which is in the standard library from C++17 onwards. It can replace many uses of unions and union-like classes.