爲什麼編譯器會產生錯誤?爲什麼編譯器會產生錯誤?
template<class T>
void ignore (const T &) {}
void f() {
ignore(std::endl);
}
編譯器VS2008給出了以下錯誤:cannot deduce template argument as function argument is ambiguous
。
爲什麼編譯器會產生錯誤?爲什麼編譯器會產生錯誤?
template<class T>
void ignore (const T &) {}
void f() {
ignore(std::endl);
}
編譯器VS2008給出了以下錯誤:cannot deduce template argument as function argument is ambiguous
。
我認爲這個問題是std::endl
是一個模板函數,編譯器不能推導出ignore
函數的模板參數。
template <class charT, class traits>
basic_ostream<charT,traits>& endl (basic_ostream<charT,traits>& os);
要解決,你可以編寫類似如下的問題:
void f() {
ignore(std::endl<char, std::char_traits<char>>);
}
但是你應該知道,你會通過函數指針作爲參數,不會導致函數執行的。
std :: endl是一個函數模板。有關更多信息,請參閱此similar question。
你有什麼錯誤? – Glen 2009-12-03 11:02:46
無法推導模板參數,因爲函數參數不明確 – 2009-12-03 11:04:29
Downvote?爲什麼downvote? – 2009-12-03 11:07:02