2009-12-03 92 views
2

爲什麼編譯器會產生錯誤?爲什麼編譯器會產生錯誤?

template<class T> 
void ignore (const T &) {} 

void f() { 
    ignore(std::endl); 
} 

編譯器VS2008給出了以下錯誤:cannot deduce template argument as function argument is ambiguous

+3

你有什麼錯誤? – Glen 2009-12-03 11:02:46

+0

無法推導模板參數,因爲函數參數不明確 – 2009-12-03 11:04:29

+3

Downvote?爲什麼downvote? – 2009-12-03 11:07:02

回答

6

我認爲這個問題是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>>); 
} 

但是你應該知道,你會通過函數指針作爲參數,不會導致函數執行的。

2

std :: endl是一個函數模板。有關更多信息,請參閱此similar question

相關問題