c++ - C 和 C++ 中反斜杠后的空白字符

标签 c++ c

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/

相关文章:

c++ - 如何删除qt5中的子目录

c++ - 从 C++ std::vector 中删除第 i 个项目

c - gdk_pixbuf_xlib_get_from_drawable 的链接器错误

c - 如何从我的库中调用可执行文件中的函数?

c - 是否可以在 Rust 中包装 C 枚举?

c - 为什么此代码不适用于 [100] 及以上? (C 中的欧拉项目 2)

C++ - 什么是 "type transparent class"?

c++ - 在 DDD 中隐藏静态数据成员,或者只显示某些成员

c++ - Octave 的 QuantLib 绑定(bind)​​?

C 编程 getc 和 while 循环行为