每当gcc
无法找到具有多个重载的函数的匹配重载,它会给出一行又一行的错误,解释尝试了哪个重载以及为什么不使用它。
虽然它通常很有用,但也常常没有用,因为问题是调用站点上的一个简单的拼写错误。在这种特殊情况下,它甚至没有帮助,因为甚至需要相当长的时间才能找出哪条线路最终对这个问题负责。
是否有任何命令行切换到 GCC 来缩短输出并仅包含实际的触发行?例如:
#include <string>
#include <iostream>
struct Z{};
void foo() {
std::string s;
Z z;
std::cout << z; // typo - meant s
}
查看错误输出:https://godbolt.org/g/wz5vL2 https://godbolt.org/g/wz5vL2
小额补充:第三方解决方案(STLFilt、gccfilter 等)不符合要求,因为 a) 我的工作环境不欢迎安装第 3 方应用程序,b) 它们往往会变得无人维护,并停止使用下一个编译器升级
一种方法是使用-Wfatal-errors
。它将错误消息更改为
<source>: In function 'void foo()':
<source>:11:15: error: no match for 'operator<<' (operand types are 'std::ostream {aka std::basic_ostream<char>}' and 'Z')
std::cout << z; // typo - meant s
~~~~~~~~~~^~~~
In file included from /opt/compiler-explorer/gcc-7.3.0/include/c++/7.3.0/iostream:39:0,
from <source>:2:
/opt/compiler-explorer/gcc-7.3.0/include/c++/7.3.0/ostream:108:7: note: candidate: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(std::basic_ostream<_CharT, _Traits>::__ostream_type& (*)(std::basic_ostream<_CharT, _Traits>::__ostream_type&)) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
operator<<(__ostream_type& (*__pf)(__ostream_type&))
many more lines of errors
to
<source>: In function 'void foo()':
<source>:11:15: error: no match for 'operator<<' (operand types are 'std::ostream {aka std::basic_ostream<char>}' and 'Z')
std::cout << z; // typo - meant s
~~~~~~~~~~^~~~
compilation terminated due to -Wfatal-errors.
Compiler returned: 1
唯一的缺点是你只会得到第一个错误。如果您的编译时间很长,那么这并不是最好的,因为在修复第一个错误并重新编译之前,您将无法修复任何其他错误。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)