Few questions about the C++ preprocessor:

心已入冬 提交于 2019-12-13 09:48:45

问题


A few questions about the C++ preprocessor:

  1. how to make the preprocessor go to a new line into the preprocessoring code?
  2. how to make the preprocessor insert a tab character or multiple spaces into the preprocessoring code?
  3. how to make the preprocessor insert comments into the preprocessoring code?

回答1:


1) use the backslash, as Tim pointed out

2) I don't think you can

3)

#define COMMENT /##/ this is a comment
#define CPPCOMMENT(c) /##/ c
#define CCOMMENT(c) /##* c *##/

COMMENT
CPPCOMMENT(This is a c++ comment)
CCOMMENT(This is a c comment)

Edit

2 Caveats

1) Doesn't work in all compilers.

2) Don't do this, it's stupid.




回答2:


Regarding #3, it's the preprocessor's responsibility to remove comments from the code, I don't think it's allowed to leave them in. In any case this would be a flag specific to the C++ compiler you're using so you should specify your environment.




回答3:


Questions 2) and 3) don't make much sense, as other people have outlined.

As for question 1, I assume what you mean is multi-line macros, which can be done in this way:

#define FOO line 1 \
    line 2  \
    line 3  \
    ...     \
    line n

Note the missing \ at the last line!




回答4:


how to make the preprocessor go to a new line into the preprocessoring code?

Why?

how to make the preprocessor insert a tab character or multiple spaces into the preprocessoring code?

Why?

how to make the preprocessor insert comments into the preprocessoring code?

Why?

The preprocessor is a pre processor, it runs before the code is converted into machine code. The whitespace and comments you want to add will have no effect on output of the application.

If you're trying to control the output of gcc -E, or something similar then you're barking up the wrong tree.




回答5:


You are doing it wrong... PREPROCESSOR is not for that purpose.



来源:https://stackoverflow.com/questions/4007865/few-questions-about-the-c-preprocessor

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!