J
jeff
Given:
===> Program <===
$ cat > macro_delay_expansion.c
#define args(a,b) a, b
#define myprintf(a, b, c) do { \
printf("%d %d %d\n", a, b, c );\
} while(0)
#define nodelay(a, b) myprintf(a, b)
#define delaymacro_expand(a, b) nodelay(a, ##b)
int main() {
delaymacro_expand(1, args(2, 3));
}
===> Error <===
$ gcc macro_delay_expansion.c
macro_delay_expansion.c:8:1: pasting "," and "args" does not give a
valid preprocessing token
$ /usr/local/remote/packages/icc_remote/9.1.046patched/bin/icc
macro_delay_expansion.c && echo success
success
$ ./a.out
1 2 3
===> Compiler Version <===
gcc (GCC) 3.4.5 20051201 (Red Hat 3.4.5-2)
icc (ICC) 9.1 20070510
Compilers on aix, hpux and solaris also accept the syntax. Just that
gcc
does not.
HP (-Aa for strict ansi compliance)
$ /usr/local/packages/compiler_remote/ansicA.06.14_aCC.A.06.14/opt/
ansic/bin/cc -Aa a.c && ./a.out
1 2 3
AIX
$ $ORACLE_HOME/bin/oraxlc a.c && ./a.out
1 2 3
Solaris
$ /usr/local/packages/studio_remote/11/SUNWspro/bin/cc a.c && ./a.out
"a.c", line 10: warning: implicit function declaration: printf
1 2 3
IMHO, gcc is right: neither args(2,3) nor nodelay should be
expanded before the token pasting happens.
Thanks,
Jeff
===> Program <===
$ cat > macro_delay_expansion.c
#define args(a,b) a, b
#define myprintf(a, b, c) do { \
printf("%d %d %d\n", a, b, c );\
} while(0)
#define nodelay(a, b) myprintf(a, b)
#define delaymacro_expand(a, b) nodelay(a, ##b)
int main() {
delaymacro_expand(1, args(2, 3));
}
===> Error <===
$ gcc macro_delay_expansion.c
macro_delay_expansion.c:8:1: pasting "," and "args" does not give a
valid preprocessing token
$ /usr/local/remote/packages/icc_remote/9.1.046patched/bin/icc
macro_delay_expansion.c && echo success
success
$ ./a.out
1 2 3
===> Compiler Version <===
gcc (GCC) 3.4.5 20051201 (Red Hat 3.4.5-2)
icc (ICC) 9.1 20070510
Compilers on aix, hpux and solaris also accept the syntax. Just that
gcc
does not.
HP (-Aa for strict ansi compliance)
$ /usr/local/packages/compiler_remote/ansicA.06.14_aCC.A.06.14/opt/
ansic/bin/cc -Aa a.c && ./a.out
1 2 3
AIX
$ $ORACLE_HOME/bin/oraxlc a.c && ./a.out
1 2 3
Solaris
$ /usr/local/packages/studio_remote/11/SUNWspro/bin/cc a.c && ./a.out
"a.c", line 10: warning: implicit function declaration: printf
1 2 3
IMHO, gcc is right: neither args(2,3) nor nodelay should be
expanded before the token pasting happens.
Thanks,
Jeff