Token pasting problem

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
 
H

Harald van Dijk

Given:

===> Program <===

#define delaymacro_expand(a, b) nodelay(a, ##b)
[...]
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
[...]
Compilers on aix, hpux and solaris also accept the syntax. Just that gcc
does not.
[...]
IMHO, gcc is right: neither args(2,3) nor nodelay should be
expanded before the token pasting happens.

If token pasting does not result in a valid token, the behaviour is
undefined by the C standard, meaning no diagnostic is required. From what
I can see, gcc is right, but the other compilers are equally right,
because they are free to do whatever they want with the code.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top