Reputation: 9240
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
EDIT2: I add this to Item
and it compiles:
Item(Item&& other) _NOEXCEPT : mMember(std::move(other.mMember))
{
}
Am I OK? Is that it?
Upvotes: 4
Views: 695
Reputation: 8494
In VS2013 default and deleted functions and rvalue references are partially implemented. Upgrade to VS2015 where these feature according to Microsoft are fully implemented (your example compiles fine). C++11/14/17 Features In VS 2015 RC
Upvotes: 2