endl
is a manipulator, i.e. it's an unresolved function type. There are several overloads, and the type deduction is unable to decide which one you want.
More specificly, here's what endl
looks like (in GNU libc++):
/**
* @brief Write a newline and flush the stream.
*
* This manipulator is often mistakenly used when a simple newline is
* desired, leading to poor buffering performance. See
* http://gcc.gnu.org/onlinedocs/libstdc++/manual/bk01pt11ch25s02.html
* for more on this subject.
*/
template<typename _CharT, typename _Traits>
inline basic_ostream<_CharT, _Traits>&
endl(basic_ostream<_CharT, _Traits>& __os)
{ return flush(__os.put(__os.widen('\n'))); }
Updated So, the problem is, the compiler cannot deduce which instance of endl
you would be passing (it's an unresolved overload). You might work around this by doing a static_cast<ostream&(*)(ostream&)>(endl)
instead.
Of course, that's not convenient. Here's a simple fix: http://liveworkspace.org/code/2F2VHe$1
#include <iostream>
using std::cout;
using std::endl;
class Foo : public std::ostream
{
public:
template<typename T>
Foo& operator<<(T&& t) {
cout << std::forward<T>(t);
return *this;
}
typedef std::ostream& (manip)(std::ostream&);
Foo& operator<<(manip& m) {
cout << m;
return *this;
}
};
int main() {
Foo foo;
foo << "Hello World"; // perfectly fine
foo << endl; // everything is fine
return 0;
}