Disabled out stream (operator overloading problem)

From: Martin Magnusson (martin_at_-xx-blecket-xx-.org)
Date: 08/12/04

Date: Thu, 12 Aug 2004 13:40:40 +0200

I have defined a number of custom stream buffers with corresponding in
and out streams for IO operations in my program, such as IO::output,
IO::warning and IO::debug. Now, the debug stream should be disabled in a
release build, and to do that efficiently, I suppose I need to overload
the << operator.

My current implementation of the stream in release mode is posted below.

Everything works fine for POD type like bool and int, but the problem is
that I have another class which has a friend operator <<, providing
output. It seems that no matter what I do, my Mesh << operator (which
produces no output, since it is sent to a Disabled_Out_Stream, but still
takes some time) is called instead of the Disabled_Out_Stream one (which
does nothing).

How can I make sure that anything that is sent to a Disabled_Out_Stream
is handled by an "empty" << operator?

#include <iostream>

class Disabled_Stream_Buffer: public std::streambuf
   Disabled_Stream_Buffer() {}

class Disabled_Out_Stream : public std::ostream
     std::ostream( new Disabled_Stream_Buffer )

   std::ostream& operator<< (bool& val ){}
   std::ostream& operator<< (short& val ){}
   std::ostream& operator<< (unsigned short& val ){}
   std::ostream& operator<< (int& val ){}
   std::ostream& operator<< (unsigned int& val ){}
   std::ostream& operator<< (long& val ){}
   std::ostream& operator<< (unsigned long& val ){}
   std::ostream& operator<< (float& val ){}
   std::ostream& operator<< (double& val ){}
   std::ostream& operator<< (long double& val ){}
   std::ostream& operator<< (void*& val ){}
   std::ostream& operator<< (std::streambuf& sb ){}
   std::ostream& operator<< (std::ostream& ( *pf )(std::ostream&)){}
   std::ostream& operator<< (std::ios& ( *pf )(std::ios&)){}
   std::ostream& operator<< (std::ios_base& ( *pf )(std::ios_base&)){}

   Should these go here?
   std::ostream& operator<< ( char ch ){}
   std::ostream& operator<< ( signed char ch ){}
   std::ostream& operator<< ( unsigned char ch ){}

   std::ostream& operator<< ( const char* str ){}
   std::ostream& operator<< ( const signed char* str ){}
   std::ostream& operator<< ( const unsigned char* str ){}

   /* Should these go here?
   std::ostream& operator<< (Disabled_Out_Stream& os, char ch ){}
   std::ostream& operator<< (Disabled_Out_Stream& os, signed char ch ){}
   std::ostream& operator<< (Disabled_Out_Stream& os, unsigned char ch ){}

   std::ostream& operator<< (Disabled_Out_Stream& os, const char* str ){}
   std::ostream& operator<< (Disabled_Out_Stream& os, const signed char*
str ){}
   std::ostream& operator<< (Disabled_Out_Stream& os, const unsigned
char* str ){}

static Disabled_Out_Stream debug;

class Mesh
   friend std::ostream& operator<< (std::ostream& o, const Mesh& m);
   int a;

std::ostream& operator<< (std::ostream& o, const Mesh& m)
   return o << "This is a mesh." << m.a;

int main()
   Mesh m;

   std::cout << m;

   for (int i = 0; i < 1e6; ++i)
     debug << m;

   return 0;