C ++使用反斜杠的多行注释 [英] C++ Multi-line comments using backslash

查看:370
本文介绍了C ++使用反斜杠的多行注释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

//样式注释可以通过使用反斜线(如多行宏)继续到下一行吗?例如

Can // style comments be continued to the next line by using a back slash, like multi-line macros? E.g.

// here is a comment \
   and this is more comments \
const char* x = "hello";  // this line of "code" is actually still a comment
int x = 5; // and now an actual line of code


推荐答案

\ 终止的行在翻译过程的早期与下一行拼接在一起。

Yes. Lines terminated by a \ are spliced together with the next line very early in the process of translation. It happens at phase 2 of translation, before comment removal and before preprocessor has a chance to do its work.

在阶段3进行注释识别和删除。因此,在注释删除之前和预处理器有机会完成其工作之前,您可以使用 \ // 注释变为多行注释。

Comment recognition and removal takes place at phase 3. For this reason you can turn a // comment into what looks like a multi-line comment by using the \. This usually fools most syntax-highlighting source code parsers.

预处理器在第4阶段工作。

Preprocessor works at phase 4.

这一切都意味着您可以使用 \ (包括注释和预处理程序指令)几乎任何东西多线

This all means that you can "multiline" virtually anything using the \, including comments and preprocessor directives

#\
d\
e\
f\
i\
n\
e \
ABC \
int i

int main() {
A\
B\
C = 5;
}

请注意,终止 \ 不会在拼接线中引入任何空格。在使用 \ 功能编写多行注释时应考虑这一点。例如,以下注释

P.S. Please note that the terminating \ does not introduce any whitespace into the spliced line. This should be taking onto account when writing multi-line comments using the \ feature. For example, the following comment

// to\
get\
her

代表单个单词together,而不是三个单独的单词get her。显然,在评论中不正确使用 \ 可能会彻底模糊,甚至扭曲其意图。

stands for the single word "together" and not for three separate words "to get her". Obviously, incorrect use of \ in comments might drastically obfuscate and even distort their intended meaning.

这篇关于C ++使用反斜杠的多行注释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆