Clang
Yks*_*nen 5
Clang-Tidy 试图警告您,您的复制构造函数无法调用基类的复制构造函数。GCC 在下面有类似的警告-Wextra:
<source>: In copy constructor 'Node::Node(const Node&)':<source>:10:5: warning: base class 'class std::enable_shared_from_this<Node>' should be explicitly initialized in the copy constructor [-Wextra] 10 | Node(const Node &that) : parent(that.parent) { if (that.parent) that.parent->children.insert(shared_from_this()); } | ^~~~您应该在成员初始值设定项列表中显式使用复制构造函数:
Node(const Node &that) : enable_shared_from_this(that), parent(that.parent) { if (that.parent) that.parent->children.insert(shared_from_this()); }但是,您使用enable_shared_from_this. 首先,您必须始终公开继承该类:
class Node : public std::enable_shared_from_this<Node>// ~~~~~~~~~~^shared_from_this()此外,当所有权std::shared_ptr尚未构造时(尤其是在类构造函数中),您不得调用。在 C++17 中,这样做会抛出std::bad_weak_ptr,在早期的标准版本中,这是未定义的行为。
Clang