unique_ptr basic 1-2: When pass value into a function, 1) for exitunique_ptr, use move to transfer ownership, 2) for newunique_ptr, use make_unique to assure exception safe.
unique_ptr basic 4-1: If a program attempts to assign one unique_ptr to
another. The compiler allows it if the source object is a temporary rvalue (It
will call move ctor or assignment of unique_ptr inside.) and disallows it if
the source object has some duration. It is a move-only type.
by default, std::unique_ptrs are the same size as raw pointers, so efficiency
of it is just like raw pointer.
If your vector should hold std::unique_ptr<Fruit> instead of raw pointers
(to prevent memory leaks). vector need copy in and copy out. but
unique_ptr don’t support copy. So 1) you can use emplace_back with new
pointer, but it will lead memory leak if extending vector size fail. 2) use
unname unique_ptr, 3) use make_unique .
When in doubt, prefer unique_ptr by default, and you can always later
move-convert to shared_ptr if you need it. If you do know from the start
you need shared ownership, however, go directly to shared_ptr via
make_shared. If you compiler report error about unique_ptr, then you
should consider to use shared_ptr
Unique_ptr has new [] version. The existence of std::unique_ptr for arrays
should be of only intellectual interest to you, because std::array, std::vector,
and std::string are virtually always better data structure choices
than raw arrays. About the only situation I can conceive of when a
std::unique_ptr<T[]> would make sense would be when you’re using a
C-like API that returns a raw pointer to a heap array that you assume
ownership of it.
A common use for std::unique_ptr is as a factory function return type for
objects in a hierarchy. detail can be seen "effective modern c++ item
18"
During construction, std::unique_ptr objects can be configured
to use custom deleters: arbitrary functions (or function objects,
including those arising from lambda expressions) to be invoked
when it’s time for their resources to be destroyed. when a custom
deleter can be implemented as either a function or a captureless
lambda expression, the lambda is preferable.
When a custom deleter is to be used, its type must be specified
as the second type argument to std::unique_ptr.
std::unique_ptr is the C++11 way to express exclusive ownership, but one
of its most attractive features is that it easily and efficiently converts
to a std::shared_ptr: This is a key part of why std::unique_ptr is
so well suited as a factory function return type. Factory functions
can’t know whether callers will want to use exclusive ownership
semantics for the object they return or whether shared ownership
unique_ptr can be used inside of class. Below class B disable value-copying
(or to define a suitable copy-constructor and operator= to handle it safely).
You can store unique_ptr objects in an STL container providing you don’t
invoke methods or algorithm, such as copy(), that copy or assign one
unique_ptr to another. see effective stl item 8.