do {...} while(false)

You can break out of do{...}while(false).


A lot of people point out that it's often used with break as an awkward way of writing "goto". That's probably true if it's written directly in the function.

In a macro, OTOH, do { something; } while (false) is a convenient way to FORCE a semicolon after the macro invocation, absolutely no other token is allowed to follow.

And another possibility is that there either once was a loop there or iteration is anticipated to be added in the future (e.g. in test-driven development, iteration wasn't needed to pass the tests, but logically it would make sense to loop there if the function needed to be somewhat more general than currently required)


The break as goto is probably the answer, but I will put forward one other idea.

Maybe he wanted to have a locally defined variables and used this construct to get a new scope.

Remember while recent C++ allows for {...} anywhere, this was not always the case.

Tags:

C++