【发布时间】: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 标志。