关于 C++ 的几个问题;预处理器:

发布于 2024-09-28 15:53:19 字数 125 浏览 2 评论 0原文

关于C++预处理器的几个问题:

  1. 如何让预处理器在预处理代码中换行?
  2. 如何让预处理器在预处理代码中插入制表符或多个空格?
  3. 如何让预处理器在预处理代码中插入注释?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

疾风者 2024-10-05 15:53:19

关于#3,预处理器有责任从代码中删除注释,我认为不允许将它们保留在其中。无论如何,这将是特定于您所使用的 C++ 编译器的标志。使用所以你应该指定你的环境。

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.

┼── 2024-10-05 15:53:19

正如其他人所概述的那样,问题 2)和 3)没有多大意义。

至于问题1,我假设你的意思是多行宏,可以这样完成:

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

注意最后一行缺少的\

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!

老旧海报 2024-10-05 15:53:19

1) 使用反斜杠,正如 Tim 指出的那样

2) 我认为你不能

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)

编辑

2 个注意事项

1) 不适用于所有编译器。

2)不要这样做,这是愚蠢的。

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.

十雾 2024-10-05 15:53:19

如何让预处理器进入预处理代码的新行?

为什么?

如何让预处理器在预处理代码中插入制表符或多个空格?

为什么?

如何让预处理器在预处理代码中插入注释?

为什么?

预处理器是一个预处理处理器,它在代码转换为机器代码之前运行。您想要添加的空格和注释不会对应用程序的输出产生影响。

如果您试图控制 gcc -E 的输出,或者类似的东西,那么您就可以吠叫错误的树。

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.

欲拥i 2024-10-05 15:53:19

你做错了......预处理器不是用于此目的的。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文