gpt4 book ai didi

c++ - -Weffc++ 对带有 shared_ptr 的简单结构的警告

转载 作者:太空宇宙 更新时间:2023-11-04 13:17:13 24 4
gpt4 key购买 nike

我尝试用 std::shared_ptr 编译非常简单的树节点。在我的编译器选项中,我使用了 -Weffc++-Werror 但它抛出了 2 个我不理解的错误,因此我无法想象解决方案。

最小示例 (t.cpp):

#include <memory>

struct node {
std::shared_ptr<node> left;
std::shared_ptr<node> right;
std::shared_ptr<int> value;
};

int main() {
node n;
return 0;
}

编译器的输出是:

$ LANG=en_US g++ -std=c++14 -Weffc++ t.cpp
t.cpp: In constructor 'constexpr node::node()':
t.cpp:3:8: warning: 'node::left' should be initialized in the member initialization list [-Weffc++]
struct node {
^
t.cpp:3:8: warning: 'node::right' should be initialized in the member initialization list [-Weffc++]
t.cpp:3:8: warning: 'node::value' should be initialized in the member initialization list [-Weffc++]
t.cpp: In function 'int main()':
t.cpp:10:10: note: synthesized method 'constexpr node::node()' first required here
node n;
^

我能找到的唯一类似的东西是 this question但不幸的是,它没有回答我的问题。

最佳答案

启用 Effective C++ 警告后,编译器会发出警告,表明您没有遵循更喜欢对初始化列表中的成员字段进行显式初始化的指南。

添加显式构造函数可能会摆脱这种情况:

node() : left(), right(), value()
{}

关于c++ - -Weffc++ 对带有 shared_ptr 的简单结构的警告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36808866/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com