17-11-2010, 11:06 AM
I would recommend the committee to rephrase rule 19.4 so that it does not allow do-while-zero inside function-like macros.
The do-while-zero mechanism has only two purposes:
The above code is a "hack" to stay compliant with rule 14.4 (goto shall not be used). But it surely leads to even less readable code than goto does.
The above is a "hack" that only applies to code non-compliant to rule 14.9 (if shall be followed by a compound statement). The sole purpose of do-while-zero is to save non-compliant code from the compiler errors it would otherwise receive! If the macro was written as two braces without the do while, there would be a compiler error because of the semicolon.
To sum this up: if code is to conform with (required) rules 14.4 and 14.9, then there is no reason for rule 19.4 to allow do-while-zero.
The do-while-zero mechanism has only two purposes:
Code:
/* "goto wrapper" */
do
{
if(something)
{
break;
}
...
} while(0);
Code:
/* "if-statement fiasco saver" */
#define FUNC(x) do
{
do_this();
do_that();
} while(0) /* no semicolon here, as enforced by 19.4 */
if(something)
FUNC(X); /* the macro allows the semicolon to be placed here */
else
To sum this up: if code is to conform with (required) rules 14.4 and 14.9, then there is no reason for rule 19.4 to allow do-while-zero.
<t></t>