On Wed, Sep 07, 2016 at 01:26:35PM +0200, Vincent Lefevre wrote: > > Firstly, I'll note that, given the existing macros don't seem to have > > caused anyone any grief, the exact macro syntax is largely an academic > > consideration. =8^) > > Not really. Code gets modified. So, bugs may appear in the future. > This happened in MPFR: my new code wasn't working and I was wondering > why... until I found that someone who wrote macros forgot parentheses.
Sure, but if it ever happened, the coder would notice that it broke
when the change was made, and fix it as required. And the existing
macros have been in Mutt for a very long time.
> The example I gave was invalid C code with the above macro, and it
> becomes valid if do ... while(0) is used. Here's a simple example:
But:
$ cat macro.c
#include <stdio.h>
#define foo(A) ({ printf("%s\n", (A)); })
int main(int argc, char **argv)
{
if (argc) foo("argc is nonzero");
else foo("argc is zero");
return 0;
}
$ gcc -Wall -Werror -o macro macro.c
$ ./macro
argc is nonzero
This works, at least for GCC, in all contexts; but I have no eperience
with (or at the moment, access to) other compilers so I can't guess
how portable it is.
Like I said, I'm familiar with the do {} while (0) construct, but I
could swear I ran into an odd case where it caused some sort of
problem... I just can't recall the details. I also think it's a
little gross, and I only compile with gcc, so I have the liberty of
using something slightly less gross, and do so.
Then again, I tend to dislike macros in general, and would rather use
a function, and inline it if possible/sensible. If your program is
performance-sensitive enough to worry about setting up the function
call, then you'll probably also want to eliminate the extra branch
instructions that the do/while construct might (or might not)
generate, depending on compiler optimizations.
--
Derek D. Martin http://www.pizzashack.org/ GPG Key ID: 0xDFBEAD02
-=-=-=-=-
This message is posted from an invalid address. Replying to it will result in
undeliverable mail due to spam prevention. Sorry for the inconvenience.
pgpVyFH5xRFT9.pgp
Description: PGP signature
