P99
|
◆ for()
Prefer the statements in the argument list over the statement or block that follows. The argument list should consist of a possibly empty list of statements, exactly as you would put them inside a The dependent statement or block will in general not be executed unless they contain a valid jump target that is jumped to. Such a jump target may be a normal label or a case label. char *str = malloc(25);
if (!str) goto CLEANUP;
.
.
P99_PREFER(fprintf(stderr, "Happy: all allocation went well!\n");)
CLEANUP: {
// Do some repair work and exit gracefully
fprintf(stderr, "Unhappy: something went wrong!\n");
}
Only execute the depending statement or block if it is jumped into explicitly from the outer block. This can be used to comment out code temporarily at source level. This macro is preferable to the common This can also be used to handle some exceptional cases to which you want to jump explicitly, either by a With this the example from ::P99_PREFER reads simply char *str = malloc(25);
if (!str) goto CLEANUP;
.
.
CLEANUP: {
// Do some repair work and exit gracefully
fprintf(stderr, "Unhappy: something went wrong!\n");
}
Execute the statements in the argument list. This is to have several statements executed in a place where syntactically only one statement (and not a The argument list should consist of a possibly empty list of statements, exactly as you would put them inside a Traditionally this would be done with a construction like do { __VA_ARGS__ } while(0)
That traditional construction changes the control flow in that
An exclusive This switch(errno) {
case 0: break; // everything works fine
P99_XDEFAULT : {
fprintf(stderr, "AUTSCH: call to schnoeck failed with unhandled case!\n");
perror("AUTSCH");
}
P99_XCASE ENOMEM :
fprintf(stderr, "Autsch: call to schnoeck didn't have enough memory!\n");
severe_error_occured = true;
P99_XCASE EINTR : {
fprintf(stderr, "Autsch: call to schnoeck was interrupted!\n");
// do something else in that case
}
// common clean up code comes here
errno = 0;
}
Please observe that these macros prefix exactly one statement or block and not a series of statements as a normal
The default case analogous to P99_XCASE Definition at line 327 of file p99_block.h. |