public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* the problem about different treatment to __VA_ARGS__ when using VS   2008 and GCC
@ 2010-04-05 21:00 Liu Liu
  2010-04-07 15:36 ` Ian Lance Taylor
  0 siblings, 1 reply; 4+ messages in thread
From: Liu Liu @ 2010-04-05 21:00 UTC (permalink / raw)
  To: gcc-help

Hi,

I am trying to identify a problem because of an unusual usage of
variadic macros. Here is the hypothetic macro:

#define va(c, d, ...) c(d, __VA_ARGS__)
#define var(a, b, ...)  va(__VA_ARGS__, a, b)

var(2, 3, printf, “%d %d %d\n”, 1);

For gcc, the preprocessor will output

printf("%d %d %d\n", 1, 2, 3)

but for VS 2008, the output is

printf, “%d %d %d\n”, 1(2, 3);

I suspect the difference is caused by the different treatment to
__VA_ARGS__, for gcc, it will first expand the expression to
va(printf, "%d %d %d\n", 1, 2, 3), and treat 1, 2, 3 as the
__VA_ARGS__ for macro va. But for VS 2008, it will first treat b as
__VA_ARGS__ for macro va, and then do the expansion.

Which one is correct interpretation for C99 variadic macro? or my
usage falls into an undefined behavior? Thank you.

--
Liu Liu
+1 650 521 3058
http://liuliu.me/
http://www.cs.virginia.edu/~ll2ef/

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: the problem about different treatment to __VA_ARGS__ when using VS   2008 and GCC
  2010-04-05 21:00 the problem about different treatment to __VA_ARGS__ when using VS 2008 and GCC Liu Liu
@ 2010-04-07 15:36 ` Ian Lance Taylor
  0 siblings, 0 replies; 4+ messages in thread
From: Ian Lance Taylor @ 2010-04-07 15:36 UTC (permalink / raw)
  To: Liu Liu; +Cc: gcc-help

Liu Liu <ll2ef@virginia.edu> writes:

> I am trying to identify a problem because of an unusual usage of
> variadic macros. Here is the hypothetic macro:
>
> #define va(c, d, ...) c(d, __VA_ARGS__)
> #define var(a, b, ...)  va(__VA_ARGS__, a, b)
>
> var(2, 3, printf, “%d %d %d\n”, 1);
>
> For gcc, the preprocessor will output
>
> printf("%d %d %d\n", 1, 2, 3)
>
> but for VS 2008, the output is
>
> printf, “%d %d %d\n”, 1(2, 3);
>
> I suspect the difference is caused by the different treatment to
> __VA_ARGS__, for gcc, it will first expand the expression to
> va(printf, "%d %d %d\n", 1, 2, 3), and treat 1, 2, 3 as the
> __VA_ARGS__ for macro va. But for VS 2008, it will first treat b as
> __VA_ARGS__ for macro va, and then do the expansion.
>
> Which one is correct interpretation for C99 variadic macro? or my
> usage falls into an undefined behavior? Thank you.

I'm pretty sure that gcc is right.  The C99 standard says that first
you replace the macro with its definition and do all argument
substitution.  Then you rescan the resulting text for any further
macros.

Ian

^ permalink raw reply	[flat|nested] 4+ messages in thread

* the problem about different treatment to __VA_ARGS__ when using VS   2008 and GCC
@ 2010-04-05 21:13 Liu Liu
  0 siblings, 0 replies; 4+ messages in thread
From: Liu Liu @ 2010-04-05 21:13 UTC (permalink / raw)
  To: gcc-help

Hi,

I am trying to identify a problem because of an unusual usage of
variadic macros. Here is the hypothetic macro:

#define va(c, d, ...) c(d, __VA_ARGS__)
#define var(a, b, ...)  va(__VA_ARGS__, a, b)

var(2, 3, printf, “%d %d %d\n”, 1);

For gcc, the preprocessor will output

printf("%d %d %d\n", 1, 2, 3)

but for VS 2008, the output is

printf, “%d %d %d\n”, 1(2, 3);

I suspect the difference is caused by the different treatment to
__VA_ARGS__, for gcc, it will first expand the expression to
va(printf, "%d %d %d\n", 1, 2, 3), and treat 1, 2, 3 as the
__VA_ARGS__ for macro va. But for VS 2008, it will first treat b as
__VA_ARGS__ for macro va, and then do the expansion.

Which one is correct interpretation for C99 variadic macro? or my
usage falls into an undefined behavior? Thank you.


--
Liu Liu
http://liuliu.me/

^ permalink raw reply	[flat|nested] 4+ messages in thread

* the problem about different treatment to __VA_ARGS__ when using VS   2008 and GCC
@ 2010-04-05 21:03 Liu Liu
  0 siblings, 0 replies; 4+ messages in thread
From: Liu Liu @ 2010-04-05 21:03 UTC (permalink / raw)
  To: gcc-help

Hi,

I am trying to identify a problem because of an unusual usage of
variadic macros. Here is the hypothetic macro:

#define va(c, d, ...) c(d, __VA_ARGS__)
#define var(a, b, ...)  va(__VA_ARGS__, a, b)

var(2, 3, printf, “%d %d %d\n”, 1);

For gcc, the preprocessor will output

printf("%d %d %d\n", 1, 2, 3)

but for VS 2008, the output is

printf, “%d %d %d\n”, 1(2, 3);

I suspect the difference is caused by the different treatment to
__VA_ARGS__, for gcc, it will first expand the expression to
va(printf, "%d %d %d\n", 1, 2, 3), and treat 1, 2, 3 as the
__VA_ARGS__ for macro va. But for VS 2008, it will first treat b as
__VA_ARGS__ for macro va, and then do the expansion.

Which one is correct interpretation for C99 variadic macro? or my
usage falls into an undefined behavior? Thank you.

--
Liu Liu
+1 650 521 3058
http://liuliu.me/
http://www.cs.virginia.edu/~ll2ef/

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2010-04-07 15:36 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2010-04-05 21:00 the problem about different treatment to __VA_ARGS__ when using VS 2008 and GCC Liu Liu
2010-04-07 15:36 ` Ian Lance Taylor
2010-04-05 21:03 Liu Liu
2010-04-05 21:13 Liu Liu

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).