gpt4 book ai didi

c++ - Pedantic : What Is A Source File? 什么是 header ?

转载 作者:IT老高 更新时间:2023-10-28 22:20:57 26 4
gpt4 key购买 nike

就这个问题而言,我只对符合标准的 C++ 感兴趣,而不是 C 或 C++0x,也不对任何特定于实现的细节感兴趣。

Questions不时出现关于#include ""之间的差异和 #include <> .该论点通常归结为两个差异:

  1. 特定的实现通常会为这两种形式搜索不同的路径。这是特定于平台的,不在此问题的范围内。
  2. 标准说 #include <>用于“标题”,而 #include ""用于“源文件”。以下是相关引用:

ISO/IEC 14882:2003(E)

16.2 Source file inclusion [cpp.include]

1 A #include directive shall identify a header or source file that can be processed by the implementation.

2 A preprocessing directive of the form

# include  < h-char-sequence > new-line
searches a sequence of implementation-defined places for a header identified uniquely by the specified sequence between the < and > delimiters, and causes the replacement of that directive by the entire contents of the header. How the places are specified or the header identified is implementation-defined.

3 A preprocessing directive of the form

# include "q-char-sequence" new-line
causes the replacement of that directive by the entire contents of the source file identified by the specified sequence between the " delimiters. The named source file is searched for in an implementation-defined manner. If this search is not supported, or if the search fails, the directive is reprocessed as if it read
# include  < h-char-sequence > new-line
with the identical contained sequence (including > characters, if any) from the original directive.

(上面引用的重点是我的。)这种差异的含义似乎是标准打算区分“标题”和“源文件”,但该文档没有定义这些术语或它们之间的区别。

很少有其他地方甚至提到头文件或源文件。一些:

158) A header is not necessarily a source file, nor are the sequences delimited by in header names necessarily valid source file names (16.2).

似乎暗示头文件可能不驻留在文件系统中,但也没有说源文件存在。

2 Lexical conventions [lex]

1 The text of the program is kept in units called source files in this International Standard. A source file together with all the headers (17.4.1.2) and source files included (16.2) via the preprocessing directive #include, less any source lines skipped by any of the conditional inclusion (16.1) preprocessing directives, is called a translation unit. [Note: a C + + program need not all be translated at the same time. ]

这是我能找到的最接近定义的,它似乎暗示标题不是“程序的文本”。但是如果你 #include一个标题,它不会成为程序文本的一部分吗?这有点误导。

那么什么是标题?什么是源文件?

最佳答案

我的阅读是标准标题,包括使用 <>尖括号,不必是文件系统上的实际文件;例如一个实现可以自由地启用一组提供 iostream 功能的“内置”操作。当它看到 #include <iostream> .

另一方面,#include "xxx.h" 中包含的“源文件”旨在成为驻留在文件系统上的文字文件,以某种依赖于实现的方式进行搜索。

编辑:为了回答您的具体问题,我相信“标题”仅限于 #include标准中规定的能力设施:iostream , vector和 friend ——或者作为标准的扩展来实现。 “源文件”可以是程序员可以编写或使用的任何非标准工具(如 .h 文件等)。

关于c++ - Pedantic : What Is A Source File? 什么是 header ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3469588/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com