C 和 C++ 标准对反斜杠后的空白字符(或几个字符)有何规定?它是否保证无论如何都会加入行?
int main()
{
// Comment \
int foo;
}
在这种情况下,MSVC 和 gcc 的工作方式不同。
最佳答案
作为引用,标准引述是(§2.2/1,删节,强调我的):
Phases of Translation
[...]
2. 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. If, as a result, a character sequence that matches the syntax of a universal-character-name is produced, 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.
[...]
其他答案提到的实现定义部分在“换行”的定义中。
(请注意,直到第 3 阶段才替换注释,因此在此代码中:
int main()
{
int x = 0;
// assuming the definition of new-line is as expected, this function
// will return 0, not 5 (no whitespace after this backslash: ) \
x = 5;
return x;
}
x = 5;
将附加到评论的末尾,然后最终删除。)
关于c++ - C 和 C++ 中反斜杠后的空白字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12305240/