View と、View がその Shape オブジェクトを「所有」する Shape クラスがあります。これをunique_ptrのベクトルとして実装しています。関数 View::add_shape(std::unique_ptr&& shape) では、rvalue パラメーターで std::move を使用してコンパイルする必要があります。なんで?(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));
}