Sorry about all of the no-impending-doom code-practice-centered questions lately. I figure there's no better place to ask. The computer science guys don't talk to us physicists on campus (I think we've abused them enough).
I've always learned that, in C++, using macros (and hiding code in a non-type-safe way in general) is evil. However, I've come up with a little short hand that seems innocuous. Please show me how this macro could be dangerous, and/or propose a better solution.
Code:
#define THE_DEBUG_STREAM std::cout
#ifndef NDEBUG
#define PRINT_TO_DEBUG_STREAM( x ) { (THE_DEBUG_STREAM) << "{ " << (#x) << " }: " << (x) << '\n'; }
#else
#define PRINT_TO_DEBUG_STREAM( x ) { (x); }
#endif
#define printdb PRINT_TO_DEBUG_STREAM
Ex:
Code:
int function()
{
return 17;
}
int main()
{
int variable;
printdb(variable = function());
}
Output:
Code:
{ variable = function() }: 17
The only issue I could think of is all of the pieces of code like this
that would be littered throughout my program ifdef NDEBUG..... but that's skipped over, right?