I have code as following
vector<unique_ptr<int>> v;
v.insert(v.end(), new int(1)); // this is okay
v.push_back(new int(1)); // this is wrong, cannot convert int* to unique_ptr<int>&&
Why does the compilation (vc2010) show the differences? Thanks.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
That’s because in VS2010,
v.insert(v.end(), new int(1));is optimized to callstd::vector::emplace_backwhich construct object in place while std::vector::push_back will try to copy/convertint*tostd::unique_ptr<int>then it failed. To make push smart pointers into STL container you can specify exact type:Or simply call
I’ve tested your code on VS2010 and VS2012, howerver VS2012 disallows
v.insert(v.end(), new int(1));, but emplace_back works in both cases.