c++ - #include "<> "和#include < ""> 是有效的文件包含吗?

标签 c++ language-lawyer

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

在 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/

相关文章:

c - "A conforming compiler may choose not to implement non-normalized floating point numbers"的意义是什么?

C++11 对 lambda 返回类型的限制

c++ - 指针互转换与具有相同地址

c++ - std::less<void> 和指针类型

c++ - ListView_GetItemText 只获取第一个字符

c++ - cv::cudacodec::VideoReader 无法播放 rtsp 流

c++ - 为什么我可以在不重载 "="运算符的情况下将一个对象分配给另一个对象?

c++ - 是否需要显式构造 char 数组的每个成员?

C++:在指针中存储常量多维数组

c++ - GetModuleFileNameEx - 是否可以在 char* 而不是 TCHAR[] 中获取路径?