I have a View and a Shape class where the View "owns" its Shape objects. I am implementing this as a vector of unique_ptr. In the function View::add_shape(std::unique_ptr&& shape), I still need to use std::move on the rvalue parameter to make it compile. Why? ( using GCC 4.8 )
#include <memory>
#include <vector>
using namespace std;
class Shape { };
class View
{
vector<unique_ptr<Shape>> m_shapes;
public:
void add_shape(unique_ptr<Shape>&& shape)
{
m_shapes.push_back(std::move(shape));// won't compile without the std::move
}
};
int main()
{
unique_ptr<Shape> ups(new Shape);
View v;
v.add_shape(std::move(ups));
}
See Question&Answers more detail:os