Pedantic:什么是源文件?什么是头? [英] Pedantic: What Is A Source File? What Is A Header?
问题描述
对于这个问题,我只感兴趣的是符合标准的C ++,而不是C或C ++ 0x,而不是任何实现的具体细节。
问题不时出现关于 #include
和 #include<>
之间的差异。该参数通常归结为两个区别:
- 特定实现通常搜索两种形式的不同路径。这是平台特定的,不在此问题的范围内。
- 标准说
#include<>
headers,#include
用于源文件。这是相关的参考:
ISO / IEC 14882: h1>
16.2源文件包含[cpp.include]
include指令应标识可由实现处理的头文件或源文件。
2
<$ p $形式的预处理指令p>#include< h-char-sequence>新行
搜索一系列实现定义的地方, ><<<<<<>标识符之间唯一地标识的头部,并且使得该指令被头部的全部内容替换。
3 格式为
的预处理指令code>#includeq-char-sequencenew-line
会导致该指令被源文件,由指定的序列在分隔符之间进行识别。以实现定义的方式搜索指定的源文件。如果不支持此搜索,或者搜索失败,则重新处理指令,读取
#include< h-char-sequence>新行
(上面引用的重点是我的)。这个差异似乎是标准打算区分头文件和源文件,但文档没有定义这些术语或它们之间的差异。
很少有其他地方甚至提到头文件或源文件。一些:
158)头部不一定是源文件,头部名称中定义的序列也不一定是有效的源文件名(16.2)。
似乎意味着头文件可能不驻留在文件系统中,但它不会说源文件。
< blockquote>
2词汇约定[lex]
1 程序的文本保存在称为源文件本国际标准。通过预处理指令 #include
,源文件与所有标题(17.4.1.2)和源文件(16.2)一起,减少任何条件包含(16.1)预处理指令,被称为翻译单元。 [注:一个C ++程序不需要全部同时翻译。 ]
这是我可以找到的最接近的定义,它似乎暗示标题不是程序的文本。但是如果你 #include
一个头,它不会成为程序的文本的一部分?这是有点误导。
那么什么是标题?什么是源文件?
这不是说标题可以实现为源文件,未必?对于什么是源文件,似乎非常明智的标准没有拼写出来,因为文件的许多方式实现。
For the purposes of this question, I am interested only in Standard-Compliant C++, not C or C++0x, and not any implementation-specific details.
Questions arise from time to time regarding the difference between #include ""
and #include <>
. The argument typically boils down to two differences:
- Specific implementations often search different paths for the two forms. This is platform-specific, and not in the scope of this question.
- The Standard says
#include <>
is for "headers" whereas#include ""
is for a "source file." Here is the relevant reference:
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.
(Emphasis in quote above is mine.) The implication of this difference seems to be that the Standard intends to differentiate between a 'header' and a 'source file', but nowhere does the document define either of these terms or the difference between them.
There are few other places where headers or source files are even mentioned. A few:
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).
Seems to imply a header may not reside in the filesystem, but it doesn't say that source files do, either.
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. ]
This is the closest I could find to a definition, and it seems to imply that headers are not the "text of the program." But if you #include
a header, doesn't it become part of the text of the program? This is a bit misleading.
So what is a header? What is a source file?
Isn't this saying that a header may be implemented as a source file, but there again may not be? as for "what is a source file", it seems very sensible for the standard not to spell this out, given the many ways that "files" are implemented.
这篇关于Pedantic:什么是源文件?什么是头?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!