C++11 unique_ptr 和 shared_ptr 是否能够相互转换为彼此的类型?
Does C++11 unique_ptr and shared_ptr able to convert to each other's type?
C++11 标准库是否提供任何实用工具来将 std::shared_ptr
转换为 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 (i.e., std::shared_ptr
) would be more appropriate. By returning a std::unique_ptr
, factories provide callers with the most efficient smart pointer, but they don’t hinder callers from replacing it with its more flexible sibling.
std::shared_ptr
to std::unique_ptr
is not allowed. Once you’ve turned lifetime management of a resource over to a std::shared_ptr
, there’s no changing your mind. Even if the reference count is one, you can’t reclaim ownership of the resource in order to, say, have a std::unique_ptr
manage it.
Reference: Effective Modern C++. 42 SPECIFIC WAYS TO IMPROVE YOUR USE OF C++11 AND C++14. Scott Meyers.
简而言之,您可以轻松高效地将 std::unique_ptr
转换为 std::shared_ptr
,但无法将 std::shared_ptr
转换为 std::unique_ptr
。
例如:
std::unique_ptr<std::string> unique = std::make_unique<std::string>("test");
std::shared_ptr<std::string> shared = std::move(unique);
或:
std::shared_ptr<std::string> shared = std::make_unique<std::string>("test");
给定 unique_ptr u_ptr,创建 shared_ptr s_ptr:
std::shared_ptr<whatever> s_ptr(u_ptr.release());
走另一条路是不切实际的。
我更愿意这样做。
std::unique_ptr<int> up_ = std::make_unique<int>();
std::shared_ptr<int> sp_ = std::move(up_);
C++11 标准库是否提供任何实用工具来将 std::shared_ptr
转换为 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 astd::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 (i.e.,std::shared_ptr
) would be more appropriate. By returning astd::unique_ptr
, factories provide callers with the most efficient smart pointer, but they don’t hinder callers from replacing it with its more flexible sibling.
std::shared_ptr
tostd::unique_ptr
is not allowed. Once you’ve turned lifetime management of a resource over to astd::shared_ptr
, there’s no changing your mind. Even if the reference count is one, you can’t reclaim ownership of the resource in order to, say, have astd::unique_ptr
manage it.Reference: Effective Modern C++. 42 SPECIFIC WAYS TO IMPROVE YOUR USE OF C++11 AND C++14. Scott Meyers.
简而言之,您可以轻松高效地将 std::unique_ptr
转换为 std::shared_ptr
,但无法将 std::shared_ptr
转换为 std::unique_ptr
。
例如:
std::unique_ptr<std::string> unique = std::make_unique<std::string>("test");
std::shared_ptr<std::string> shared = std::move(unique);
或:
std::shared_ptr<std::string> shared = std::make_unique<std::string>("test");
给定 unique_ptr u_ptr,创建 shared_ptr s_ptr:
std::shared_ptr<whatever> s_ptr(u_ptr.release());
走另一条路是不切实际的。
我更愿意这样做。
std::unique_ptr<int> up_ = std::make_unique<int>();
std::shared_ptr<int> sp_ = std::move(up_);