Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ debug print to stream generates warnings

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?

like image 368
Mankka Avatar asked Nov 24 '25 15:11

Mankka


1 Answers

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 );
like image 121
Seg Fault Avatar answered Nov 26 '25 06:11

Seg Fault



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!