为什么编译器会生成错误?
template<class T>
void ignore (const T &) {}
void f() {
ignore(std::endl);
}
Run Code Online (Sandbox Code Playgroud)
编译器VS2008给出以下错误:cannot deduce template argument as function argument is ambiguous.
我认为那个问题是std::endl模板函数和编译器不能推导ignore函数的模板参数.
template <class charT, class traits>
basic_ostream<charT,traits>& endl ( basic_ostream<charT,traits>& os );
Run Code Online (Sandbox Code Playgroud)
要解决问题,您可以编写如下内容:
void f() {
ignore(std::endl<char, std::char_traits<char>>);
}
Run Code Online (Sandbox Code Playgroud)
但是你应该知道你将把指针传递给函数作为参数,而不是函数执行的结果.