我只花了几个小时调试一个编译器错误,如果编译器的错误消息更有帮助,我可以立即修复它。
我把它简化为一个简单的例子:
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
我会期待这样的事情......