I am trying to find out why in the following example, when I disable copy-semantics but preserve move-semantics, emplacing into std::vector works in one case but does not work in another (when inheritance is used):
#include <iostream>
#include <vector>
class NonCopyable
{
public:
NonCopyable(const NonCopyable &) = delete;
NonCopyable(NonCopyable &&) noexcept = default;
NonCopyable &operator=(const NonCopyable &) = delete;
NonCopyable &operator=(NonCopyable &&) noexcept = default;
NonCopyable() = default;
virtual ~NonCopyable() = default;
};
// NOTE: here things works as long as I dont override the destructor. If I do, it stops.
class MyClass : public NonCopyable
{
public:
MyClass() = default;
// NOTE: when commented out, all compiles fine, otherwise not
~MyClass() override {}
};
// NOTE: when all is put into a single class, everything compiles ok
class MyClass2
{
public:
MyClass2() = default;
~MyClass2() noexcept
{
}
MyClass2(const MyClass2 &) = delete;
MyClass2 &operator=(const MyClass2 &) = delete;
MyClass2(MyClass2 &&) noexcept = default;
MyClass2 &operator=(MyClass2 &&) noexcept = default;
};
int main()
{
std::vector<MyClass> mc;
MyClass a;
mc.emplace_back(std::move(a));
std::vector<MyClass2> mc2;
MyClass2 a2;
mc2.emplace_back(std::move(a2));
}
Example in online compiler: https://onlinegdb.com/UTWju9WkU
What do I miss ? Thanks!