我刚刚花了几个小时调试编译器错误,如果编译器的错误消息更有帮助,我可以立即修复这个错误。无益(可能是错误的?)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
我本来期望类似的东西...
哪个GCC的版本在哪个平台产生的误差?这是一个G ++扩展还是新的C++ 0x语法?鉴于使用`auto`,我认为后者。 – 2011-01-23 03:59:56
@Jonathan:decltype和后期指定的返回类型是C++ 0x(因此这个问题的C++ 0x标签)的新增内容。自(至少)版本4.4以来,它们一直由gcc支持。我引用的错误是由gcc 4.6的实验版本产生的,但4.4和4.5给出了类似的错误。 – HighCommander4 2011-01-23 04:04:28