Avatar ·

Why do C++ macros use meaningless do-while and if-else?

Hi everyone! I've been writing C++ for about six months and started learning about preprocessor macros. Often in other people's code and in system header files (e.g., Boost or Qt), I see macros wrapped in strange constructs like `do { ... } while(0)` or `if (1) { ... } else (void)0`. Here's an example: ```cpp #define LOG_ERROR(msg) do { \ std::cerr << "ERROR: " << msg << std::endl; \ } while(0) #define SAFE_DELETE(p) if (p) { delete p; p = nullptr; } else (void)0 ``` At first glance, this looks completely pointless: why use a `do-while` that is guaranteed to execute exactly once? Why not just write a block of code in curly braces? And what's with the strange `else (void)0` at the end of the second macro? I tried writing my own macro without these wrappers: ```cpp #def