C++ debug print to stream generates warnings

568 views Asked by At

I saw this debug print for c++ here on stackoverflow but I can't comment it (I'm a newbie):

#ifdef DEBUG
#define dout cout
#else
#define dout 0 && cout
#endif

It is used like this:

dout << "in foobar with x= " << x << " and y= " << y << '\n';

At first sight I liked it, but I compile with -Wall, so I get a lot of warnings like

test1.cc:30:46: warning: statement has no effect [-Wunused-value]

Is there a way to reconcile -Wall and the stream oriented debug print?

1

There are 1 answers

0
Seg Fault On

This can be further refined, but try this as starting point:

#ifdef DEBUG
#define LOG_DEBUG( stuff ) { std::cout << stuff << std::endl; }
#else
#define LOG_DEBUG( stuff )
#endif

Then later in the code:

LOG_DEBUG( __FILE__ << " bla bla bla " << foo );