3

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?

Community
  • 1
  • 1
Mankka
  • 521
  • 3
  • 12

1 Answers1

2

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 );
Community
  • 1
  • 1
Seg Fault
  • 1,331
  • 8
  • 16