这很挑剔,可能没有任何实际用途。我只是好奇...
在 C++20 工作草案 (n4861) 中, header 名称定义为:
(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 " <> "
和 #include < "" >
完全有效,一个在 h-char-sequence
之后, 另一个在 q-char-sequence
之后.
然而,这对我来说似乎相当奇怪和奇怪。我尝试创建一个名为 <>
的头文件在 visual studio 中(恕我直言)禁止这样做,通知我不能将这样的头文件创建为两个字符 <
和 >
被禁止。
严格来说,visual studio 在这方面是遵循标准还是我忽略了什么?两个 header 名称是否有效?如果无效,标准的哪一部分禁止这样做?此外,由于源文件包含的定义包含空格,因此是 #include <iostream>
甚至有效?
最佳答案
再往下一点,您会发现类似于以下的文本(取自 C99 标准):
The implementation shall provide unique mappings for sequences consisting of one or more letters or digits (as defined in 5.2.1) followed by a period (.) and a single letter. The first character shall be a letter. The implementation may ignore the distinctions of alphabetical case and restrict the mapping to eight significant characters before the period.
这自 C99 以来几乎没有变化,可能更早。因此允许实现拒绝文件名中包含奇数字符的 include 指令(例如 "
或 <
),但不是必需的。
关于c++ - #include "<> "和#include < ""> 是有效的文件包含吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61855928/