tags:

views:

146

answers:

3

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.

+6  A: 

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
+2  A: 

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

Benoît