1

Trying to make a logging system that only logs data when a certain macro is defined, I've done something like this:

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

int main()
{
    foo("Hello!");
    return 0;
}

The function main, after pre-processing, expands to:

int main()
{
    ;
    return 0;
}

However, on some places, I saw that people use do{}while(0) instead of an empty macro. I suppose that a compiler would optimize away both of these but I'm wondering is there an advantage that one has over another?

I am aware of the need for both an empty statement and do{}while(0) but what I do not know is the difference between the two.

I don't believe my question was fully read and compared to the ones that have been provided when marking as duplicate.

nm_tp
  • 463
  • 3
  • 15
  • Oh right... Here's an issue which is why they switch to do while, which is solved by using `({ ... })`, now you can use it anywhere. – Purple Ice Dec 26 '18 at 19:19
  • It would be nice to un-mark this question as a duplicate. Nonetheless, thanks. So, my empty macro is perfectly valid or should I use `({...})`? – nm_tp Dec 27 '18 at 09:46

0 Answers0