【问题标题】:Are #include " <> " and #include < "" > valid file inclusions?#include " <> " 和 #include < "" > 是有效的文件包含吗?
【发布时间】:2020-05-17 17:26:46
【问题描述】:

这很挑剔,可能没有任何实际用途。我只是好奇...

从 C++20 工作草案 (n4861) 中,标题名称定义为:

(5.8)
header-name:
    < h-char-sequence >
    " q-char-sequence "
h-char-sequence :
    h-char
    h-char-sequence h-char
h-char:
    any member of the source character set except new-line and >
q-char-sequence :
    q-char
    q-char-sequence q-char
q-char:
    any member of the source character set except new-line and "

其中“源字符集”定义为:

(5.3.1) The basic source character set consists of 96 characters: the space character, the control characters representing
horizontal tab, vertical tab, form feed, and new-line, plus the following 91 graphical characters:9
  a b c d e f g h i j k l m n o p q r s t u v w x y z
  A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
  0 1 2 3 4 5 6 7 8 9
  _ { } [ ] # ( ) < > % : ; . ? * + - / ^ & | ~ ! = , \ " ’

源文件包含定义为:

(15.3.2) A preprocessing directive of the form
    # include < h-char-sequence > new-line
    ...

(15.3.3) A preprocessing directive of the form
    # include " q-char-sequence " new-line
    ...

据我了解,这意味着包含指令#include " &lt;&gt; " 和#include &lt; "" &gt; 都是完全有效的,一个在h-char-sequence 之后,另一个在q-char-sequence 之后。

然而,这对我来说似乎相当奇怪和奇怪。我尝试在 Visual Studio 中创建一个名为 &lt;&gt; 的头文件,该文件(恕我直言)禁止这样做,通知我不能创建这样的头文件,因为字符 &lt; 和 &gt; 都被禁止。

严格来说,Visual Studio 在这方面是遵循标准还是我忽略了什么?两个标头名称是有效还是无效?如果无效,标准的哪一部分禁止这样做?另外,由于源文件包含的定义包含一个空格,#include &lt;iostream&gt; 是否有效?

【问题讨论】:

标签: c++ language-lawyer


【解决方案1】:

再往下一点,您会发现类似于以下的文本(取自 C99 标准):

实现应为由一个或一个组成的序列提供唯一的映射 多个字母或数字(如 5.2.1 中定义)后跟句点 (.) 和单个字母。 第一个字符应该是一个字母。实现可能会忽略 字母大小写,并将映射限制为 8 个有效字符之前 期间。

自 C99 以来,甚至可能更早之前,这几乎没有变化。因此允许实现拒绝包含文件名中包含奇数字符的指令(例如" 或&lt;),但不是必须的。

【讨论】:

    猜你喜欢
    • 2016-03-08
    • 2019-11-01
    • 2014-10-30
    • 1970-01-01
    • 2012-06-09
    • 2011-06-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多