If I have these objects:
struct A
{
int aa{};
A() {};
~A()
{
OutputDebugStringA("~An");
}
};
struct B : public A
{
B() {};
~B()
{
OutputDebugStringA("~Bn");
}
};
I can aggregate initialize a std::array
of A
or B
without copying:
std::array<A, 1> arr0{ A{} };
std::array<B, 1> arr1{ B{} };
BUT if I try to creat a std::array
of the base class and initialize it with the derived class:
std::array<A, 1> arr0{ B{} };
It works without error however, there is copying going on as the destructors are being called. So my question is why? And is there any reasonable way to structure it to avoid copying?