有两种常用的路径分隔符: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/