c++ - Why do we need a move constructor for vector of unique_ptr? -
it seems vc11 update 2, requires move constructor when pushing unique_ptr
's in std::vector. documented behavior or known bug?
#include < iostream> #include <memory> #include <vector> struct test { std::unique_ptr<int> m_l; test( std::unique_ptr<int>&& l) { m_l = std::move(l); }; //move contructor test test(test&& o) { m_l = std::move(o.m_l); } }; void bar() { std::vector<test> vec; std::unique_ptr<int> a(new int); //compiles fine without move constructor test(std::move(a)); //requires move contructor compile vec.push_back( test(std::move(a))); } int main() { bar(); return 0; }
note
i tried above code sans move constructor on ideone c++11 , compiles fine.
you shouldn't have write move constructor yourself; should automatically generated compiler in case. however, vc11 doesn't implement functionality , iirc isn't going added until vs2013.
note vc11 complaining because presence of std::unique_ptr
data member causes copy constructor deleted. §12.8p11 describes process of deleting class's copy constructor:
an implicitly-declared copy/move constructor inline public member of class. a defaulted copy/move constructor class x defined deleted (8.4.3) if x has:
[...]
— a non-static data member of class type m (or array thereof) cannot copied/moved because overload resolution (13.3), applied m’s corresponding constructor, results in ambiguity or a function deleted or inaccessible defaulted constructor,
[...]
Comments
Post a Comment