单行注释续延 [英] Single line comment continuation

查看:225
本文介绍了单行注释续延的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从C ++标准(回到至少C ++ 98)§2.2,注2说明:

From the C++ standard (going back to at least C++98) § 2.2, note 2 states:


每个反斜杠字符(\)后紧跟一个新行字符被删除,拼接物理源行以形成逻辑源行。只有任何物理源行上的最后一个反斜杠才有资格作为这种拼接的一部分。除了在原始字符串文字中还原的拼接之外,如果拼接导致与通用字符名称的语法匹配的字符序列,则该行为未确定。如果源文件不是空的,并且不以新行字符结束,或者在任何此类拼接发生之前以反斜杠字符开头的新行字符结束,则该文件将被处理,行字符附加到文件。
Each instance of a backslash character (\) immediately followed by a new-line character is deleted, splicing physical source lines to form logical source lines. Only the last backslash on any physical source line shall be eligible for being part of such a splice. Except for splices reverted in a raw string literal, if a splice results in a character sequence that matches the syntax of a universal-character-name, the behavior is undefined. A source file that is not empty and that does not end in a new-line character, or that ends in a new-line character immediately preceded by a backslash character before any such splicing takes place, shall be processed as if an additional new-line character were appended to the file.

并且,第2.7节规定:

And, section § 2.7 states:


字符/ * start一个注释,以* /结束。这些注释不嵌套。字符//开始注释,以下一个新行字符结束。如果在这样的注释中存在换页符或垂直标签字符,则在它和终止注释的换行之间仅出现空格字符;不需要诊断。 [注意:注释字符//,/ *和* /在//注释中没有特殊含义,并且像其他字符一样处理。类似地,注释字符//和/ *在/ *注释中没有特殊含义。 ]
The characters /* start a comment, which terminates with the characters */. These comments do not nest. The characters // start a comment, which terminates with the next new-line character. If there is a form-feed or a vertical-tab character in such a comment, only white-space characters shall appear between it and the new-line that terminates the comment; no diagnostic is required. [Note: The comment characters //, /*, and */ have no special meaning within a // comment and are treated just like other characters. Similarly, the comment characters // and /* have no special meaning within a /* comment. ]

我将这两个一起代表以下内容:

I would take these two together to mean that the following:

// My comment \
is valid

// My comment \ still valid \
is valid

在C ++ 98中是合法的。在GCC 4.9.2中,这两个都编译时没有任何诊断消息。在MSVC 2013中,这两个都生成以下内容:

are legal in C++98. In GCC 4.9.2, these both compile without any diagnostic messages. In MSVC 2013, these both produce the following:

warning C4010: single-line comment contains line-continuation character

如果您在启用错误时出现警告(我这样做),则会导致程序无法成功编译 - 错误,它的工作正常)。在标准中有不允许单行注释延续的东西,或者这是MSVC不遵守标准的情况吗?

If you have warnings as errors enabled (which, I do), this causes the program to not compile successfully (without warnings-as-errors, it works just fine). Is there something in the standard that disallows single-line comment continuations, or is this a case of MSVC non-compliance with the standard?

推荐答案

#define macro() \
    some stuff \
    // Intended as comment \
    more stuff

那么当你在代码中使用 macro()时,你会得到非常有趣的错误。

then you get VERY interesting errors when you use macro() in the code.

或者其他简单地意外键入这样的注释:

Or other simply accidentally typing a comment like this:

// The files for foo-project are in c:\projects\foo\
int blah;

(发生未定义变量blah的错误错误)

(Strange errors for "undefined variable blah" occurs)

我不会在单行注释中使用行续延,但如果你有一些很好的理由,只要在MSVC中关闭警告。

I would NEVER use line continuation in a single-line comment, but if you have some good reason to, just turn THAT warning off in MSVC.

还有Mike说:警告甚至不包括在标准中 - 它只是说什么需要是一个错误。如果启用warnings are errors,您将必须选择性地启用哪些警告,或者接受一些在技术上有效(但可疑)的构造在构建中是不可接受的,因为编译器制造商已决定警告关于它。尝试在gcc或clang中编写 if(c = getchar()),看看在high上有多少-Werror和警告。然而,根据标准,它是完全有效的。

Also as Mike says: Warnings are not even covered by the standard - it only says what needs to be an error. If you enable "warnings are errors", you will have to either be selective about what warnings you enable, or accept that some constructs that are technically valid (but dubious) will be unacceptable in the build, because the compiler maker has decided to warn about it. Try writing if (c = getchar()) in gcc or clang and see how far you get with much -Werror and warnings on "high". Yet it is perfectly valid according to the standard.

这篇关于单行注释续延的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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