This pattern is licensed under the CC0 Public Domain Dedication.
Transfer unique ownership of a dynamically allocated object to another unit of code.
On line 11, we create a
which has ownership of a dynamically allocated
(created with the
utility function). Line 12 then demonstrates passing ownership of this
object to the function
func. After passing ownership,
longer has access to the
std::unique_ptr is non-copyable, it must be moved instead of
being copied. The call to
std::move on line 12
obj to be treated like a temporary object (the expression
std::move(obj) is an rvalue) so that it can be moved into the
In other cases, you may instead wish to share ownership of an object.
std::make_unique was introduced in C++14. For C++11,
you can roll your own implementation.