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不是类,它是函数模板。无法推断模板参数,因为函数参数不明确