Reputation: 39
atest.c
#define COMMENT /##/
int main()
{
...
COMMENT int atest;
...
}
The error messages:
atest.c:16:1: error: pasting "/" and "/" does not give a valid preprocessing token
atest.c: In function 'main':
atest.c:16: error: expected expression before '/' token
While Microsoft C compiler is happy with the COMMENT macro. The tip of here doesn't work too.
Could someone give a solution or explanation for this? Thanks.
Upvotes: 3
Views: 749
Reputation: 791849
When you use the token pasting operator ##
, the result of combining the two operands must be a valid preprocessor token.
//
is not a valid preprocessing token. Preprocessing happens after comments are removed so it is not possible to add comments at the preprocessing stage.
Upvotes: 8
Reputation: 1133
I'm sorry I don't have the answer on how to do this.
You can use the command cpp (the c prepocessor) to preprocess your code and see what is happening. Running your provided code through cpp gives the following:
int main()
{
a.c:5:1: error: pasting "/" and "/" does not give a valid preprocessing token
/ / int atest;
}
Trying
#define COMMENT /
#define SLASH(x) x/
SLASH(COMMENT)
results in
/ /
However trying
#define TEST(y) y/
TEST(hello)
results in
hello/
Usually comments such as // are stripped out and replaced with a single 'comment token', this step is done before preprocessing (when # directives take place) and thus even if you could get your comment in there, I doubt it would always be recognised (AFAIK the standard doesn't guarantee it).
I'm sorry I can't be more help.
Upvotes: 2
Reputation: 42443
Because comment processing is done before macros, so the comment generated by the macro is not recognized by the compiler. Quoting the response of Ben Combee on the article you referenced:
I just read your C/C++ Tip #5, published in the January 2001 issue of CUJ. The content of the tip (doing a macro that expands into a comment) was quite familiar, since I'd seen a similar method deployed in one of Microsoft's header files. However, the tip is very incorrect.
The writer uses token pasting to construct a comment start sequence. However, this is invalid according to the C and C++ Standards. In section 5.1.1.2 of ISO C (1999), titled "Translation phases," comment processing occurs in phase 3 where the preprocessor identifies tokens and whitespace, with comments replaced with whitespace. Macro processing is then done in phase 4, and that phase is the one in which these new comment start sequences would be introduced. There is no followup phase after #4 where the comment would be recognized, so eventually a conforming compiler would reject the program text due to an illegal token — '//'. The 1998 C++ Standard has similar language in section 2.1, "Phases of translation [lex.phases]."
Some compilers mix the order and processing of phases sufficiently to allow this macro to work, Microsoft's Visual C++ 6.0 being notable for its popularity. Others, such as the Metrowerks CodeWarrior C/C++ compilers (upon which I worked for over two years as a compiler engineer before my current employment) follow the Standard and will reject the usage shown in the tip.
Thanks,
Ben Combee Lead Software Architect Veriprise Wireless http://www.veriprise.com
Accordingly, the journal followed by this notice:
We therefore want to caution readers that this tip is not portable and probably should not be used in production code.
Upvotes: 4