c++ - std::unique_ptr 结构成员到结构类型

标签 c++ unique-ptr

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

标准允许吗?我不认为它适用于像 std::set 这样的容器类型,但是关于 unique_ptr 有什么特别吗?

最佳答案

是的,这是明确允许的。 C++14 (n4140) 20.8.1/5:

... The template parameter T of unique_ptr may be an incomplete type.

std::shared_ptrstd::weak_ptr 也允许使用类似的措辞。

关于c++ - std::unique_ptr 结构成员到结构类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31410352/

相关文章:

c++ - 嵌套数据结构中的STL排序

c++ - 需要帮助解决使用模板的运行时多态性

c++ - std::make_unique<T> 与重置(新 T)

c++ - 在堆上存储 unique_ptr 的最简单方法是什么?

c++ - 我们什么时候应该在 unique_ptr 中使用自定义删除器而不是默认删除器?

c++ - C++ 新手,使用 "=="的/字符串相等性问题

c++ - 全局(静态编译)变量位于何处?

c++ - OpenGL 计算着色器调用

c++ - 如何通过 std::unique_ptr 成员调用其他类的 const 成员函数

c++ - 如何将 unique_ptr 与 operator new 一起使用