Transferring the ownership of object from one unique_ptr to another unique_ptr in C++11?

Roy picture Roy · Oct 11, 2014 · Viewed 41.6k times · Source

In C++11 we can transfer the ownership of an object to another unique_ptr using std::move(). After the ownership transfer, the smart pointer that ceded the ownership becomes null and get() returns nullptr.

std::unique_ptr<int> p1(new int(42));
std::unique_ptr<int> p2 = std::move(p1); // Transfer ownership

What are the situations where this will be useful as it is transferring the ownership to another unique_ptr?

Answer

Chris Drew picture Chris Drew · Oct 12, 2014

The following situations involve transferring ownership from one unique_ptr to another: returning from a function, and passing as a parameter to a function like a constructor.

Say you have some polymorphic type Animal:

struct Animal {
  virtual ~Animal() {}
  virtual void speak() = 0;
};

with concrete subclasses Cat and Dog:

struct Cat : Animal {
  void speak() override { std::cout << "Meow!\n"; }
};

struct Dog : Animal {
  void speak() override { std::cout << "Woof!\n"; }
};

And you want a simple factory that creates a pet based on a required value of obedience. Then the factory must return a pointer. We want the pet factory to transfer ownership of the created pet to the caller so a reasonable return type is std::unique_ptr<Animal>:

std::unique_ptr<Animal> createPet(double obedience) {
  if (obedience > 5.0)
    return std::make_unique<Dog>();
  return std::make_unique<Cat>();
} 

Now, say we want to create a House that will own the pet then we might want to pass the pet into the constructor of the House. There is some debate (see comments on this blog post) about how best to pass a unique_ptr to a constructor but it would look something like this:

class House {
 private:
  std::unique_ptr<Animal> pet_;
 public:
  House(std::unique_ptr<Animal> pet) : pet_(std::move(pet)) {}
};

We have passed the unique_ptr into the constructor and have then "moved" it to the member variable.

The calling code could look something like:

  auto pet = createPet(6.0);
  House house(std::move(pet));

After constructing the House, the pet variable will be nullptr because we have transferred ownership of the pet to the House.

Live demo