c++ - C 和 C++ #include 指令中是否可以使用反斜杠?

标签 c++ c include c-preprocessor header-files

有两种常用的路径分隔符:Unix 正斜杠和 DOS 反斜杠。 安息吧,经典 Mac 冒号。 如果在 #include 指令中使用,它们在 C++11、C++03 和 C99 的规则下是否相等标准?

最佳答案

C99 说(§6.4.7/3):

If the characters ', \, ", //, or /* occur in the sequence between the < and > delimiters, the behavior is undefined. Similarly, if the characters ', \, //, or /* occur in the sequence between the " delimiters, the behavior is undefined.

(footnote: Thus, sequences of characters that resemble escape sequences cause undefined behavior.)

C++03 说(§2.8/2):

If either of the characters ’ or \, or either of the character sequences /* or // appears in a q-char- sequence or a h-char-sequence, or the character " appears in a h-char-sequence, the behavior is undefined.

(footnote: Thus, sequences of characters that resemble escape sequences cause undefined behavior.)

C++11 说(§2.9/2):

The appearance of either of the characters ’ or \ or of either of the character sequences /* or // in a q-char-sequence or an h-char-sequence is conditionally supported with implementation-defined semantics, as is the appearance of the character " in an h-char-sequence.

(footnote: Thus, a sequence of characters that resembles an escape sequence might result in an error, be interpreted as the character corresponding to the escape sequence, or have a completely different meaning, depending on the implementation.)

因此,尽管任何编译器都可能选择在 #include 路径中支持反斜杠,但任何编译器供应商都不太可能不支持正斜杠,并且反斜杠可能会使某些实现出错通过形成转义码。 (编辑:显然 MSVC 以前需要反斜杠。也许 DOS 衍生平台上的其他人也类似。嗯……我能说什么。)

C++11 似乎放宽了规则,但“有条件地支持”并不比“导致未定义的行为”更好。这种变化更多地反射(reflect)了某些流行编译器的存在,而不是描述了一个可移植的标准。

当然,这些标准中没有任何内容表明存在路径之类的东西。 个文件系统根本没有路径!但是,许多库都采用路径名,包括 POSIX 和 Boost,因此需要一种可移植的方式来引用子目录中的文件是合理的。

关于c++ - C 和 C++ #include 指令中是否可以使用反斜杠?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5790161/

相关文章:

c++ - 多进程如何使用同一个ncurses屏幕?

c++ - 关于此代码中::* 的含义

c++ - 如何编写以负数结尾的循环 (MyProgrammingLab 11138)

python - 在 python 中将标准输出设置为非阻塞

c++ - 编译命令eclipse c++

c++: switch 语句在右括号前缺少分号

当 fork() 遇到 fopen() 时的困惑

c++ - 在 C++ 中的多个文件中包含单个 header 库

php - include() 资源(css,js)好还是让浏览器再做一个请求好?

c++ - C++ (GCC) 中的 C99 严格别名规则