2

Why does compiler generate error?

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

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

Compiler VS2008 gives the following error: cannot deduce template argument as function argument is ambiguous.

Alexey Malistov
  • 26,407
  • 13
  • 68
  • 88

3 Answers3

6

I think that problem is that std::endl is a template function and compiler cannot deduce template argument for ignore function.

template <class charT, class traits>
  basic_ostream<charT,traits>& endl ( basic_ostream<charT,traits>& os );

To fix a problem you could write something like as follows:

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

But you should know that you will pass pointer to function as argument, not result of function execution.

Kirill V. Lyadvinsky
  • 97,037
  • 24
  • 136
  • 212
2

std::endl is a function template. See this similar question for more information.

Community
  • 1
  • 1
Benoît
  • 16,798
  • 8
  • 46
  • 66