我剛剛花了幾個小時調試編譯器錯誤,如果編譯器的錯誤消息更有幫助,我可以立即修復這個錯誤。無益(可能是錯誤的?)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