简体   繁体   English

std :: unique_ptr结构成员的结构类型

[英]std::unique_ptr structure member to the structure type

Is

struct A
{
    std::unique_ptr<A> a;
};

allowed by the standard? 标准允许的? I don't think it is for container types like std::set but is there something special about unique_ptr ? 我认为它不适用于像std::set这样的容器类型,但是unique_ptr有什么特别之处吗?

Yes, it's explicitly allowed. 是的,明确允许。 C++14 (n4140) 20.8.1/5: C ++ 14(n4140)20.8.1 / 5:

... The template parameter T of unique_ptr may be an incomplete type. ... unique_ptr的模板参数T可以是不完整类型。

It is also allowed for std::shared_ptr and std::weak_ptr , using similar wording. 它也允许std::shared_ptrstd::weak_ptr ,使用类似的措辞。

Sure it's allowed. 当然这是允许的。 This basically an implementation of some kind of path . 这基本上是某种路径的实现

According to this 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. 可以为不完整类型T构造std :: unique_ptr,以便于在Pimpl习语中用作句柄。 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. 如果使用默认删除器,则必须在调用删除器的代码中完成T,这发生在析构函数,移动赋值运算符和std :: unique_ptr的重置成员函数中。

So yes std::unique_ptr can be used in this way. 所以是的, std::unique_ptr可以这种方式使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM