2009-12-03 77 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

相关问题