-Wfatal-errors 做你想做的事吗?
它会在第一个错误之后停止所有错误,这与简单地抑制候选函数注释不同,但它会显着减少输出:
$ cat a.cc
void f() { }
void f(int) { }
void f(char) { }
int main()
{
f((void*)0);
}
$ g++ a.cc
a.cc: In function ‘int main()’:
a.cc:7: error: call of overloaded ‘f(void*)’ is ambiguous
a.cc:2: note: candidates are: void f(int) <near match>
a.cc:3: note: void f(char) <near match>
$ g++ a.cc -Wfatal-errors
a.cc: In function ‘int main()’:
a.cc:7: error: call of overloaded ‘f(void*)’ is ambiguous
compilation terminated due to -Wfatal-errors.
或者,如果您想修补 GCC,这会添加一个 -fno-candidate-functions 开关:
--- gcc/c-family/c.opt.orig 2012-07-11 16:37:29.373417154 +0000
+++ gcc/c-family/c.opt 2012-07-11 17:09:47.340418384 +0000
@@ -752,6 +752,10 @@
fbuiltin-
C ObjC C++ ObjC++ Joined
+fcandidate-functions
+C++ ObjC++ Var(flag_candidates) Init(1)
+-fno-candidate-functions Do not print candidate functions when overload resolution fails
+
fcheck-new
C++ ObjC++ Var(flag_check_new)
Check the return value of new
--- gcc/cp/call.c.orig 2012-07-11 17:08:34.186424089 +0000
+++ gcc/cp/call.c 2012-07-11 17:09:51.843444951 +0000
@@ -3317,6 +3317,9 @@
for (n_candidates = 0, cand1 = candidates; cand1; cand1 = cand1->next)
n_candidates++;
+ if (!flag_candidates)
+ return;
+
inform_n (loc, n_candidates, "candidate is:", "candidates are:");
for (; candidates; candidates = candidates->next)
print_z_candidate (loc, NULL, candidates);
--- gcc/cp/pt.c.orig 2012-07-11 16:37:35.658636650 +0000
+++ gcc/cp/pt.c 2012-07-11 17:10:20.910435942 +0000
@@ -1751,9 +1751,12 @@
void
print_candidates (tree fns)
{
- const char *str = NULL;
- print_candidates_1 (fns, false, &str);
- gcc_assert (str == NULL);
+ if (flag_candidates)
+ {
+ const char *str = NULL;
+ print_candidates_1 (fns, false, &str);
+ gcc_assert (str == NULL);
+ }
}
/* Returns the template (one of the functions given by TEMPLATE_ID)