the class foo has rvalue reference constructor moves contained vector of unique_ptr's why following code give following error, both or without std::move on foo() in main?
error 1 error c2280: 'std::unique_ptr> &std::unique_ptr<_ty,std::default_delete<_ty>>::operator =(const std::unique_ptr<_ty,std::default_delete<_ty>> &)' : attempting reference deleted function
class foo{ public: foo(){ } foo(foo&& other) : m_bar(std::move(other.m_bar)) {}; std::vector<std::unique_ptr<something>> m_bar; }; int main(int argc, char* argv[]) { foo f; f = std::move(foo()); return 0; }
this:
f = std::move(foo()); doesn't call move constructor. calls move assignment operator. furthermore, it's redundant, since foo() rvalue that's equivalent to:
f = foo(); since declared move constructor, move assignment operator isn't declared - there isn't one. either have provide one:
foo& operator=(foo&& other) { m_bar = std::move(other.m_bar); return *this; } or, since members implement move operations themselves, delete move constructor , rely on compiler-generated implicit move constructor , move assignment.
Comments
Post a Comment