无用的(也许是错误的?)gcc 错误消息

发布于 2024-10-13 07:10:54 字数 921 浏览 7 评论 0原文

我刚刚花了几个小时调试一个编译器错误,如果编译器的错误消息更有帮助的话,我可以立即修复该错误。

我将其简化为一个简单的示例:

template <typename T>
int f(int);

template <typename U>
auto g(U x) -> decltype(f(x));

int main()
{
    g(0);
}

错误是:

test.cpp: In function 'int main()':
test.cpp:9:8: error: no matching function for call to 'g(int)'
test.cpp:9:8: note: candidate is:
test.cpp:5:29: note: template<class U> decltype (f(x)) g(U)

这个错误最多不是误导性的,最坏的是完全错误的吗?在我看来,问题不是给定的 g 定义与调用不匹配,而是定义格式错误(因为在 decltype 中的表达式 f(x) 中,它尝试调用 f 而不指定 f 的模板参数)。

难道不是更合理的错误消息是这样的:

no matching function for call to 'f(int)' in 'decltype(f(x))'
in instantiation of 'g(U)' with U = int

甚至更好:

failed to deduce template parameter 1 in call to 'f(int)' in 'decltype(f(x))'
in instantiation of 'g(U)' with U = int

我本来期望这样的事情......

I just spent a couple of hours debugging a compiler error that I could have fixed immediately if the compiler's error message had been more helpful.

I've reduced it to a simple example:

template <typename T>
int f(int);

template <typename U>
auto g(U x) -> decltype(f(x));

int main()
{
    g(0);
}

The error is:

test.cpp: In function 'int main()':
test.cpp:9:8: error: no matching function for call to 'g(int)'
test.cpp:9:8: note: candidate is:
test.cpp:5:29: note: template<class U> decltype (f(x)) g(U)

Is this error not at best misleading and at worst, outright wrong? The way I see it, the problem is not that the given definition of g is not a match for the call, but that the definition is malformed (since in the expression f(x) in the decltype, it tries to call f without specifying f's template parameter).

Wouldn't a much more reasonable error message be something like:

no matching function for call to 'f(int)' in 'decltype(f(x))'
in instantiation of 'g(U)' with U = int

or even better:

failed to deduce template parameter 1 in call to 'f(int)' in 'decltype(f(x))'
in instantiation of 'g(U)' with U = int

I would have expected something like that...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

任性一次 2024-10-20 07:10:54

您很可能遇到了 C++0x 中的“扩展 SFINAE”规则;因为对 f(x) 的调用在 g 返回类型的实例化中不起作用(因为无法推导 Tf 的调用),g 具有无效的返回类型,因此会默默地从重载集中删除。尽管它会损害错误消息质量,但这是一项功能,因为编译器假设 g 是您不打算调用的不相关函数。在这种情况下,没有其他 g 重载,因此编译器应该给出更好的消息。

有关扩展 SFINAE 的更多信息,请访问 http: //www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2634.html

You are most likely hitting the "extended SFINAE" rules in C++0x; since the call to f(x) is not working within the instantiation of the return type of g (because of the inability to deduce T for the call to f), g has an invalid return type and thus is removed from the overload set silently. This is a feature, despite its harm to error message quality, because the compiler is assuming that g is an unrelated function that you aren't intending to call. In this case, there are no other overloads of g, so the compiler should give a better message, though.

There is more information on extended SFINAE available at http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2634.html.

梦冥 2024-10-20 07:10:54

使用 Clang 我得到这个错误

C:\Users\SUPER USER\Desktop>clang++ -cc1 -std=c++0x aa.cpp
aa.cpp:9:5: error: no matching function for call to 'g'
    g(0);
    ^
aa.cpp:5:6: note: candidate template ignored: substitution failure [with U = int
]
auto g(U x) -> decltype(f(x)){}
     ^
1 error generated.

比 g++ 产生的错误更容易理解

With Clang I get this error

C:\Users\SUPER USER\Desktop>clang++ -cc1 -std=c++0x aa.cpp
aa.cpp:9:5: error: no matching function for call to 'g'
    g(0);
    ^
aa.cpp:5:6: note: candidate template ignored: substitution failure [with U = int
]
auto g(U x) -> decltype(f(x)){}
     ^
1 error generated.

Much easier to understand than the error produced by g++

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文