std::unique_ptr 结构成员到结构类型
std::unique_ptr structure member to the structure type
是
struct A
{
std::unique_ptr<A> a;
};
标准允许吗?我不认为它适用于像 std::set
这样的容器类型,但是关于 unique_ptr
有什么 特殊的 吗?
当然允许。这基本上是某种 path.
的实现
是的,这是明确允许的。 C++14 (n4140) 20.8.1/5:
... The template parameter T
of unique_ptr
may be an incomplete type.
std::shared_ptr
和 std::weak_ptr
也允许使用类似的措辞。
根据这个reference:
std::unique_ptr may be constructed for an incomplete type T, such as
to facilitate the use as a handle in the Pimpl idiom. If the default
deleter is used, T must be complete at the point in code where the
deleter is invoked, which happens in the destructor, move assignment
operator, and reset member function of std::unique_ptr.
所以是std::unique_ptr
可以这样用
是
struct A
{
std::unique_ptr<A> a;
};
标准允许吗?我不认为它适用于像 std::set
这样的容器类型,但是关于 unique_ptr
有什么 特殊的 吗?
当然允许。这基本上是某种 path.
的实现是的,这是明确允许的。 C++14 (n4140) 20.8.1/5:
... The template parameter
T
ofunique_ptr
may be an incomplete type.
std::shared_ptr
和 std::weak_ptr
也允许使用类似的措辞。
根据这个reference:
std::unique_ptr may be constructed for an incomplete type T, such as to facilitate the use as a handle in the Pimpl idiom. If the default deleter is used, T must be complete at the point in code where the deleter is invoked, which happens in the destructor, move assignment operator, and reset member function of std::unique_ptr.
所以是std::unique_ptr
可以这样用