Connecting Tech Pros Worldwide Help | Site Map

debug function -> noop

John Hunter
Guest
 
Posts: n/a
#1: Jul 22 '05

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

Gernot Frisch
Guest
 
Posts: n/a
#2: Jul 22 '05

re: debug function -> noop



"John Hunter" <jdhunter@ace.bsd.uchicago.edu> schrieb im Newsbeitrag
news:m2smclcarq.fsf@mother.paradise.lost...[color=blue]
>
> In C++, what is the best what to define a function / macro /[/color]
template[color=blue]
> which will print a string to std::cout if a flag is set, but[/color]
generate[color=blue]
> no code otherwise. In my code I want to be able to do something[/color]
like[color=blue]
>
> DEBUG_PRINT("some string");
>
> but I want to define DEBUG_PRINT in such a way that code is
> conditionally generated.
>
> Thanks!
> John Hunter
>[/color]

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


Peter van Merkerk
Guest
 
Posts: n/a
#3: Jul 22 '05

re: debug function -> noop


Gernot Frisch wrote:
[color=blue]
> "John Hunter" <jdhunter@ace.bsd.uchicago.edu> schrieb im Newsbeitrag
> news:m2smclcarq.fsf@mother.paradise.lost...
>[color=green]
>>In C++, what is the best what to define a function / macro /[/color]
>
> template
>[color=green]
>>which will print a string to std::cout if a flag is set, but[/color]
>
> generate
>[color=green]
>>no code otherwise. In my code I want to be able to do something[/color]
>
> like
>[color=green]
>>DEBUG_PRINT("some string");
>>
>>but I want to define DEBUG_PRINT in such a way that code is
>>conditionally generated.[/color]
>
>
> #ifdef _DEBUG
> #define DEBUG_PRINT(a) std::cout << (a)
> #else
> #define DEBUG_PRINT(a)
> #endif[/color]

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.

--
Peter van Merkerk
peter.van.merkerk(at)dse.nl
Gernot Frisch
Guest
 
Posts: n/a
#4: Jul 22 '05

re: debug function -> noop


> I believe using the NDEBUG macro would be a bit more standard way to
do[color=blue]
> it. The standard does mention NDEBUG, but doesn't mention _DEBUG.[/color]

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


Closed Thread