debug function -> noop

J

John Hunter

In C++, what is the best what to define a function / macro / template
which will print a string to std::cout if a flag is set, but generate
no code otherwise. In my code I want to be able to do something like

DEBUG_PRINT("some string");

but I want to define DEBUG_PRINT in such a way that code is
conditionally generated.

Thanks!
John Hunter
 
G

Gernot Frisch

John Hunter said:
In C++, what is the best what to define a function / macro / template
which will print a string to std::cout if a flag is set, but generate
no code otherwise. In my code I want to be able to do something like

DEBUG_PRINT("some string");

but I want to define DEBUG_PRINT in such a way that code is
conditionally generated.

Thanks!
John Hunter

#ifdef _DEBUG
#define DEBUG_PRINT(a) std::cout << (a)
#else
#define DEBUG_PRINT(a)
#endif
 
P

Peter van Merkerk

Gernot said:
#ifdef _DEBUG
#define DEBUG_PRINT(a) std::cout << (a)
#else
#define DEBUG_PRINT(a)
#endif

I believe using the NDEBUG macro would be a bit more standard way to do
it. The standard does mention NDEBUG, but doesn't mention _DEBUG.
 
G

Gernot Frisch

I believe using the NDEBUG macro would be a bit more standard way to
do
it. The standard does mention NDEBUG, but doesn't mention _DEBUG.

Sorry, must be one of the MS specifics I trapped into...
Thank you,
Gernot
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top