考虑以下C ++代码:
#include <string>
struct A {
A(const std::string& s): s(s) {}
std::string s;
};
struct B: A {
using A::A;
};
int main() {
B b("test");
}
当我通过带有-Wuseless-cast
参数的GCC 6.2.1进行编译时
g++ -std=c++14 -Wuseless-cast test.cpp -o test
它发出以下警告:
test.cpp: In constructor ‘B::B(const string&)’:
test.cpp:9:14: warning: useless cast to type ‘const string& {aka const std::__cxx11::basic_string<char>&}’ [-Wuseless-cast]
using A::A;
^
test.cpp: In function ‘int main()’:
test.cpp:13:15: note: synthesized method ‘B::B(const string&)’ first required here
B b("test");
^
但是,当我将的定义更改B
为
struct B: A {
B(const std::string& s): A(s) {}
};
警告消失了。
问题:
B
而不是从中继承它可以A
解决警告?您的示例可以进一步简化为:
struct A {
A(const int& i): i(i) {}
int i;
};
struct B: A {
using A::A;
};
int main() {
B b(0);
}
这是海湾合作委员会的一个公开问题。
包括<string>
没有明显需要复制它。请注意,该问题仍未得到确认,已知至少会影响GCC 6.1-通过查看您的问题,我会说它也影响GCC 6.2。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句