【问题标题】:Why does compiler generate error?为什么编译器会产生错误?
【发布时间】:2009-12-03 11:01:25
【问题描述】:

编译器为什么会报错?

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

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

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

【问题讨论】:

  • 无法推断模板参数,因为函数参数不明确

标签: c++ templates stl


【解决方案1】:

我认为问题在于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 了解更多信息。

    【讨论】:

      【解决方案3】:
      猜你喜欢
      • 1970-01-01
      • 2023-03-22
      • 1970-01-01
      • 2021-08-18
      • 1970-01-01
      • 2012-01-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多