无用的(也许是错误的?)gcc 错误消息
我刚刚花了几个小时调试一个编译器错误,如果编译器的错误消息更有帮助的话,我可以立即修复该错误。
我将其简化为一个简单的示例:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您很可能遇到了 C++0x 中的“扩展 SFINAE”规则;因为对
f(x)
的调用在g
返回类型的实例化中不起作用(因为无法推导T
对f
的调用),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 ofg
(because of the inability to deduceT
for the call tof
),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 thatg
is an unrelated function that you aren't intending to call. In this case, there are no other overloads ofg
, 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.
使用 Clang 我得到这个错误
比 g++ 产生的错误更容易理解
With Clang I get this error
Much easier to understand than the error produced by g++