Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 为什么编译器会产生错误?_C++_Templates_Stl - Fatal编程技术网

C++ 为什么编译器会产生错误?

C++ 为什么编译器会产生错误?,c++,templates,stl,C++,Templates,Stl,为什么编译器会产生错误 template<class T> void ignore (const T &) {} void f() { ignore(std::endl); } 模板 无效忽略(常量T&){} void f(){ 忽略(std::endl); } 编译器VS2008给出以下错误:无法推断模板参数,因为函数参数不明确我认为问题在于std::endl是一个模板函数,编译器无法推断忽略函数的模板参数 template <class charT, c

为什么编译器会产生错误

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

void f() {
   ignore(std::endl);
}
模板
无效忽略(常量T&){}
void f(){
忽略(std::endl);
}

编译器VS2008给出以下错误:
无法推断模板参数,因为函数参数不明确

我认为问题在于
std::endl
是一个模板函数,编译器无法推断
忽略
函数的模板参数

template <class charT, class traits>
  basic_ostream<charT,traits>& endl ( basic_ostream<charT,traits>& os );
模板
basic_ostream&endl(basic_ostream&os);
要解决问题,您可以编写如下内容:

void f() {
   ignore(std::endl<char, std::char_traits<char>>);
}
void f(){
忽略(std::endl);
}

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

std::endl是函数模板。有关更多信息,请参见此


std::endl不是类,它是函数模板。

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