Reputation: 13
I am trying to put RenderTargetView into an union defined below.
But when I try to do that I get the error for the no default constructor available. I am sure there should be default constructor because everything is defined.
Also if I put RenderTargetView outside of the union I am no longer getting that error. Does anyone know what is happening here ?
Eroror message:
GP::Private::ResourceView::ResourceView(void)': attempting to reference a deleted function Function was implicitly deleted because 'GP::Private::ResourceView' has a variant data member 'GP::Private::ResourceView::RTView' with a non-trivial default constructor
struct RenderTargetView
{
static constexpr uint32_t INVALID_INDEX = static_cast<uint32_t>(-1);
uint32_t Index = INVALID_INDEX;
};
struct FailingStruct
{
union
{
RenderTargetView RTView;
};
};
Upvotes: 1
Views: 211
Reputation: 75698
A union has default non deleted constructor only if all members of the union have trivial constructors. Same for destructors. Since your struct has a member initializer this means that it doesn't have a trivial constructor this means the union constructor is deleted. You need to create special members for union where you delegate to the active member.
Or better yet use std:: variant
which has all this created for you.
As for why it's simple: a union doesn't know which member is active so it cannot call the appropriate constructor/destructor.
Upvotes: 2