I am using Visual Studio 2012 Update 2 and am having trouble trying to understand why std::vector is trying to use the copy constructor of unique_ptr. I have looked at similar issues and most are related to not having an explicit move constructor and/or operator.
If I change the member variable to a string, I can verify that the move constructor is called; however, trying to use the unique_ptr results in the compilation error:
error C2248: 'std::unique_ptr<_Ty>::unique_ptr' : cannot access private member declared in class 'std::unique_ptr<_Ty>'
.
I'm hoping someone can point me to what I am missing, thanks!
#include <vector>
#include <string>
#include <memory>
class MyObject
{
public:
MyObject() : ptr(std::unique_ptr<int>(new int))
{
}
MyObject(MyObject&& other) : ptr(std::move(other.ptr))
{
}
MyObject& operator=(MyObject&& other)
{
ptr = std::move(other.ptr);
return *this;
}
private:
std::unique_ptr<int> ptr;
};
int main(int argc, char* argv[])
{
std::vector<MyObject> s;
for (int i = 0; i < 5; ++i)
{
MyObject o;
s.push_back(o);
}
return 0;
}