Re:C23: A Slightly Better C
by trealira on Sunday January 21, 2024 @03:25PM
(#39082666)
It could help in generic macros, like this:
Previously, you would have needed a third macro for the type, or you would have needed to do a byteswap to be generic.
[ Parent
| on HN ]
True, and now we also get typeof() so there is more than one way!
const typeof(var1) tmp = var1;Why’s that inside do/while?
If you simply put braces around it, you'd generate syntactically invalid code when you don't use braces for if-else statements.
Consider this:
if (e)
SWAP(a, b);
else
something_else();
Currently, that expands to this, which is still valid code: if (e)
do {
// ...macro...
} while(0);
else
something_else();
If it were just wrapped in braces, the code would be parsed like this: // One-armed if-statement
if (e) {
// ...macro...
}
// Empty statement
;
// Another statement
else something_else();
It's incorrect syntax to start a statement with else, so the compiler will say something like "Unexpected 'else' at line N."I would not have come up with that on my own. Thanks for the explanation!
No problem! I didn't come up with it on my own, either. I simply read it somewhere, and now I'm repeating it.
Forces the use of a semicolon:
SWAP(…);
To scope the temporaries?
Re:C23: A Slightly Better C
by peterfirefly on Sunday January 21, 2024 @05:30PM
(#39083744)
No, a block would be enough for that. It's to make ; handling natural when the macro is used.
https://www.c-faq.com/ question 10.4
"What's the best way to write a multi-statement macro?"