Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

A question on macro expansion

0 views
Skip to first unread message

Ark

unread,
Jul 22, 2004, 2:47:17 PM7/22/04
to
Hello, NG,
Please, help on this snippet:

#define CAT(a,b) a##b
#define COMMENT CAT(/,/)
COMMENT This is a comment

Should it compile?
It passes MS C/C++ 13.0 (Visual Studio 2002) and fails IAR ARM
compiler.
IAR claims that comments should not be processed after macro
expansion, so I have an odd situation where the preprocessor output is
as intended and is compilable, whereas the original file is not.

What is the truth, according to the standard(s)?
(I myself can read but cannot parse 'em :)

Thank you very much,
Ark

Eric Sosman

unread,
Jul 22, 2004, 3:07:49 PM7/22/04
to
Ark wrote:
> Hello, NG,
> Please, help on this snippet:
>
> #define CAT(a,b) a##b
> #define COMMENT CAT(/,/)
> COMMENT This is a comment
>
> Should it compile?

No.

> It passes MS C/C++ 13.0 (Visual Studio 2002) and fails IAR ARM
> compiler.
> IAR claims that comments should not be processed after macro
> expansion, so I have an odd situation where the preprocessor output is
> as intended and is compilable, whereas the original file is not.
>
> What is the truth, according to the standard(s)?
> (I myself can read but cannot parse 'em :)

Comments are removed in translation phase 3. Macros
are expanded in phase 4.

"The preprocessor output" is really just a sort of
courtesy many compilers extend. The Standard does not
require that such output be obtainable, and certainly
doesn't require that the output (if any) have the same
meaning as the code from which it was produced. This
is a case of the latter situation: The preprocessed
output is valid C, but the source from which it was
generated is not.

--
Eric....@sun.com

jacob navia

unread,
Jul 22, 2004, 3:17:21 PM7/22/04
to
This should not compile according to the standard.
According to Microsoft yes.

Problem is that windows header files use this feature to
selectiveley comment out portions of the code
according to macro definitions embedded in
#ifdefs, well the usual stuff.

This was one of the many problems with those headers.
They tend to be very Microsoft specific.


ark

unread,
Jul 22, 2004, 7:22:00 PM7/22/04
to

"jacob navia" <ja...@jacob.remcomp.fr> wrote in message
news:cdp3s1$e9d$1...@news-reader5.wanadoo.fr...
While we are at it...
#define CAT(a,b) a##b
#define CATB(a,b,c) CAT(CAT(a,b),c)
#define CATA(a,b,c) CAT(a,CAT(b,c))
Should I expect that CATB(pro, duct, ion) expand to production, and same for
CATA ?
Thanks,
- Ark


---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.722 / Virus Database: 478 - Release Date: 7/21/2004


0 new messages