Why use apparently meaningless do-while and if-else statements in macros?
In many C/C++ macros I'm seeing the code of the macro wrapped in what seems like a meaningless `do while` loop. Here are examples.
#define FOO(X) do { f(X); g(X); } while (0)
#define FOO(X) if (1) { f(X); g(X); } else
I can't see what the `do while` is doing. Why not just write this without it?
#define FOO(X) f(X); g(X)
The only thing coming to me here is that they wanted the code to be within a scope so it doesn't pollute the enclosing scope in which it is used. Anything used within those braces is only visible within the braces so if there were variables defined there they wouldn't encroach on any variables in the scope where the macro was used