A question on macro expansion

A

Ark

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
 
E

Eric Sosman

Ark said:
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.
 
J

jacob navia

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.
 
A

ark

jacob navia said:
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.
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
 

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,995
Messages
2,570,228
Members
46,818
Latest member
SapanaCarpetStudio

Latest Threads

Top