2

I am using MS Visual Studio 2019.

I have the following:

struct MyClass
{
    std::vector<int> ints{};
    std::map<int, int> map{};
};

struct MyMoveOnly
{
    MyClass obj{};
    MyMoveOnly() noexcept = default;
    ~MyMoveOnly() noexcept = default;
    MyMoveOnly(const MyMoveOnly&) = delete;
    MyMoveOnly(MyMoveOnly&& other) noexcept = default;
    MyMoveOnly& operator=(const MyMoveOnly& other) = delete;
    MyMoveOnly& operator=(MyMoveOnly&& other) noexcept = default;
};

And in my main I try to do a move as follows:

MyMoveOnly a{};
MyMoveOnly b = std::move(a);

This works fine if I comment out the map in MyClass so it only has a vector. But with the map I get this error when trying to compile: 'MyMoveOnly::MyMoveOnly(const MyMoveOnly &)': attempting to reference a deleted function

std::map is moveable so I don't get why this would happen. Is this the correct behavior or have I found a bug?

nazame
  • 41
  • 2
  • 2
    `std::map` member doesn't disable a move constructor in general - it may prevent a `noexcept` move constructor. The move constructor of `std::map` is not required to be `noexcept`; it is in some but not all implementations. – Igor Tandetnik Sep 07 '20 at 22:45

0 Answers0