是反斜线C和C ++的#include指令是否可以接受? [英] Is the backslash acceptable in C and C++ #include directives?

查看:177
本文介绍了是反斜线C和C ++的#include指令是否可以接受?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有两种常用的路径分隔符:Unix的正斜杠和DOS反斜杠。 <子> 安息吧,经典的Mac结肠。 如果#include指令使用的,是他们下的C ++ 11的规则相同,C ++ 03,和C99标准是什么?

There are two path separators in common use: the Unix forward-slash and the DOS backslash. Rest in peace, Classic Mac colon. If used in an #include directive, are they equal under the rules of the C++11, C++03, and C99 standards?

推荐答案

C99说(§6.4.7/ 3):

C99 says (§6.4.7/3):

如果字符',\\,//或/ *出现在&lt序列中;和>分隔符的行为是不确定同样,如果字符',\\ //或/ *出现分隔符之间的顺序,其行为是不确定的。

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):

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

如果任一字符或\\,或任一字符的序列/ *或//出现的q煤焦序列或H-炭序列,或字符出现在H-煤焦序列,行为是不确定的。

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):

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

的任一字符或\\或任字符的外观的序列/ *或//在Q-字符序列或的h-炭序列有条件地与实现定义语义支持,因为是外观在H-字符序列的性质。

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.

(脚注:因此,类似于一个转义序列可能导致错误的字符序列,是PTED作为对应于转义序列的字符间$ P $,或具有完全不同的含义,这取决于实现。 )

(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 路径反斜杠,这是不可能的,任何编译器供应商不支持斜线,和反斜线可能凭借形成逃生codeS的绊倒一些实现了。 (编辑:。显然MSVC previously需要反斜线也许其他人DOS衍生的平台是相似嗯...我能说什么)

Therefore, although any compiler might choose to support a backslash in a #include path, it is unlikely that any compiler vendor won't support forward slash, and backslashes are likely to trip some implementations up by virtue of forming escape codes. ( apparently MSVC previously required backslash. Perhaps others on DOS-derived platforms were similar. Hmmm… what can I say.)

C ++ 11的看起来的放松规则,但有条件地支持是不是有意义比导致未定义的行为。变化的确更多地反映了某些流行的编译器的存在,而不是描述一个便携式的标准。

C++11 seems to loosen the rules, but "conditionally supported" is not meaningfully better than "causes undefined behavior." The change does more to reflect the existence of certain popular compilers than to describe a portable standard.

当然,没有任何这些标准说有这样的事路径。有的文件系统在那里与根本没有路!然而,许多库假定路径名,包括POSIX和升压,所以是合理的希望有一个可移植的方式来引用文件子目录中。

Of course, nothing in any of these standards says that there is such a thing as paths. There are filesystems out there with no paths at all! However, many libraries assume pathnames, including POSIX and Boost, so it is reasonable to want a portable way to refer to files within subdirectories.

这篇关于是反斜线C和C ++的#include指令是否可以接受?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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