c – Noncopyable和move构造函数

我已经使一个类的成员不可复制,但我给它一个移动构造函数和赋值运算符.然而它不能用像矢量那样的容器来打球.

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;
}

以下编译错误:

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 &)'

为什么编译器不能识别会员可以被移动?我错过了什么?

编辑:Visual Studio 2013

EDIT2:我将它添加到Item并编译:

Item(Item&& other) _NOEXCEPT : mMember(std::move(other.mMember))
{
}

我还好吗?是吗?

最佳答案 在VS2013中,部分实现了默认和删除的函数和右值引用.升级到VS2015,根据Microsoft完全实现这些功能(您的示例编译正常).
C++11/14/17 Features In VS 2015 RC

点赞