为什么编译器会生成错误?

时间:2009-12-03 11:01:25

标签: c++ templates stl

为什么编译器会生成错误?

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

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

编译器VS2008出现以下错误:cannot deduce template argument as function argument is ambiguous

3 个答案:

答案 0 :(得分: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>>);
}

但是你应该知道你会将指针传递给函数作为参数,而不是函数执行的结果。

答案 1 :(得分:2)

std :: endl是一个函数模板。有关详细信息,请参阅此similar question

答案 2 :(得分:1)