By José Tomás Tocino

2010-05-10 14:29:57 8 Comments

I'm trying to write my own logging class and use it as a stream:

logger L;
L << "whatever" << std::endl;

This is the code I started with:

#include <iostream>

using namespace std;

class logger{
    template <typename T>
    friend logger& operator <<(logger& log, const T& value);

template <typename T>
logger& operator <<(logger& log, T const & value) {
    // Here I'd output the values to a file and stdout, etc.
    cout << value;
    return log;

int main(int argc, char *argv[])
    logger L;
    L << "hello" << '\n' ; // This works
    L << "bye" << "alo" << endl; // This doesn't work
    return 0;

But I was getting an error when trying to compile, saying that there was no definition for operator<< (when using std::endl):

pruebaLog.cpp:31: error: no match for ‘operator<<’ in ‘operator<< [with T = char [4]](((logger&)((logger*)operator<< [with T = char [4]](((logger&)(& L)), ((const char (&)[4])"bye")))), ((const char (&)[4])"alo")) << std::endl’

So, I've been trying to overload operator<< to accept this kind of streams, but it's driving me mad. I don't know how to do it. I've been loking at, for instance, the definition of std::endl at the ostream header file and written a function with this header:

logger& operator <<(logger& log, const basic_ostream<char,char_traits<char> >& (*s)(basic_ostream<char,char_traits<char> >&))

But no luck. I've tried the same using templates instead of directly using char, and also tried simply using "const ostream& os", and nothing.

Another thing that bugs me is that, in the error output, the first argument for operator<< changes, sometimes it's a reference to a pointer, sometimes looks like a double reference...


@Tyler McHenry 2010-05-10 14:42:14

endl is a strange beast. It isn't a constant value. It's actually, of all things, a function. You need a special override to handle the application of endl:

logger& operator<< (logger& log, ostream& (*pf) (ostream&))
  cout << pf;
  return log;

This accepts insertion of a function that takes an ostream reference and returns an ostream reference. That's what endl is.

Edit: In response to FranticPedantic's interesting question of "why can't the compiler deduce this automatically?". The reason is that if you delve yet deeper, endl is actually itself a template function. It's defined as:

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

That is, it can take any sort of ostream as its input and output. So the problem isn't that the compiler can't deduce that T const & could be a function pointer, but that it can't figure out which endl you meant to pass in. The templated version of operator<< presented in the question would accept a pointer to any function as its second argument, but at the same time, the endl template represents an infinite set of potential functions, so the compiler can't do anything meaningful there.

Providing the special overload of the operator<< whose second argument matches a specific instantiation of the endl template allows the call to resolve.

@José Tomás Tocino 2010-05-10 14:48:49

Thanks! That solved it. Now that I see your solution, I find that my definition basic_ostream<char,char_traits<char> >& (*s)(basic_ostream<char,char_traits<char> >&) which is a generic version of ostream, wasn't working because it was defined "const". Once I've dropped the const, it's worked.

@Bill Prin 2010-05-10 14:56:50

To me the real question remains, which is why can't the compiler deduce T as a function pointer and bind to it?

@T.E.D. 2010-05-10 14:39:49

In C++ it is the stream buffer that encapsulates the underlying I/O mechanisim. The stream itself only encapsulates the conversions to string, and the I/O direction.

Thus you should be using one of the predefined stream classes, rather than making your own. If you have a new target you want your I/O to go to (like a system log), what you should be creating is your own stream buffer (derived from std::streambuf).

@Mark B 2010-05-10 14:44:08

I believe that the problem is your stream doesn't overload operator<< to accept a function that has the same type as std::endl as illustrated in this answer: std::endl is of unknown type when overloading operator<<

@Jon Purdy 2010-05-10 14:40:13

endl is an IO manipulator, which is a functor that accepts a stream by reference, performs some operation on it, and returns that stream, also by reference. cout << endl is equivalent to cout << '\n' << flush, where flush is a manipulator that flushes the output buffer.

In your class, you just need to write an overload for this operator:

logger& operator<<(logger&(*function)(logger&)) {
    return function(*this);

Where logger&(*)(logger&) is the type of a function accepting and returning a logger by reference. To write your own manipulators, just write a function that matches that signature, and have it perform some operation on the stream:

logger& newline(logger& L) {
    return L << '\n';

@Bill Prin 2010-05-10 14:53:47

This is more complicated than the accepted answer but an interesting approach nonetheless.

@Jon Purdy 2010-05-10 15:03:59

@FranticPedantic: Two sides of the same coin, really.

Related Questions

Sponsored Content

18 Answered Questions

[SOLVED] How can I profile C++ code running on Linux?

  • 2008-12-17 20:29:24
  • Gabriel Isenberg
  • 505536 View
  • 1771 Score
  • 18 Answer
  • Tags:   c++ linux profiling

14 Answered Questions

[SOLVED] What is the effect of extern "C" in C++?

22 Answered Questions

[SOLVED] What is the "-->" operator in C++?

1 Answered Questions

[SOLVED] The Definitive C++ Book Guide and List

  • 2008-12-23 05:23:56
  • grepsedawk
  • 2293566 View
  • 4245 Score
  • 1 Answer
  • Tags:   c++ c++-faq

39 Answered Questions

16 Answered Questions

[SOLVED] Why doesn't Java offer operator overloading?

27 Answered Questions

[SOLVED] Easiest way to convert int to string in C++

8 Answered Questions

10 Answered Questions

7 Answered Questions

Sponsored Content