dimanche 31 mai 2015

Noncopyable and move constructor

I have made a member of a class non-copyable but I have given it a move constructor and assignment operator. Yet it dosn't play ball with a container like a vector.

class NonCopyable
{
public:
    NonCopyable(const NonCopyable&) = delete;
    NonCopyable& operator=(const NonCopyable&) = delete;


protected:
    NonCopyable()
    {
    }

    ~NonCopyable() _NOEXCEPT
    {
    }
};


class Member : NonCopyable
{
public:
    Member(int i) : mNum(i)
    {
    }
    ~Member()
    {
    }

    Member(Member&& other) _NOEXCEPT : mNum(other.mNum)
    {
    }

    Member& operator= (Member&& other) _NOEXCEPT
    {
        std::swap(mNum, other.mNum);
        return *this;
    }

private:
    int mNum;
};


struct Item
{
    Item(int i) : mMember(i)
    {
    }

    Member mMember;
};



int _tmain(int argc, _TCHAR* argv[])
{
    std::vector<Item> vec;
    vec.emplace_back(1);

    return 0;
}

The following compiler error:

error C2280: 'NonCopyable::NonCopyable(const NonCopyable &)' : attempting to reference a deleted function
see declaration of 'NonCopyable::NonCopyable'
This diagnostic occurred in the compiler generated function 'Member::Member(const Member &)'

Why dosn't the compiler recognize the Member can be moved? What am I missing?

EDIT: Visual studio 2013

Aucun commentaire:

Enregistrer un commentaire