【问题标题】:-Weffc++ warning on simple structure with shared_ptr-Weffc++ 对带有 shared_ptr 的简单结构的警告
【发布时间】:2016-04-23 09:16:22
【问题描述】:

我尝试用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,但很遗憾它没有回答我的问题。

【问题讨论】:

  • 尝试添加默认构造函数?看起来好像编译器生成的错误被推断为 constexpr
  • @RichardHodges 谢谢你,你的建议有助于清除关于 constexpr 的第二个警告。但是,russw_uk 的回答清除了所有警告。
  • 啊酷。所以它只是归结为 effc 标志。

标签: c++ c++11


【解决方案1】:

启用 Effective C++ 警告后,编译器会警告您未遵循指南,倾向于显式初始化初始化程序列表中的成员字段。

添加显式构造函数可能会摆脱这个:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-06
    • 2017-03-31
    • 2016-11-14
    • 1970-01-01
    • 1970-01-01
    • 2012-10-17
    相关资源
    最近更新 更多