Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does compiler generate error?

Tags:

c++

templates

stl

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.

like image 228
Alexey Malistov Avatar asked Dec 17 '25 23:12

Alexey Malistov


2 Answers

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.

like image 167
Kirill V. Lyadvinsky Avatar answered Dec 20 '25 12:12

Kirill V. Lyadvinsky


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

like image 29
Benoît Avatar answered Dec 20 '25 11:12

Benoît