其他分享
首页 > 其他分享> > c – “外部声明或定义”不是指我的想法吗?

c – “外部声明或定义”不是指我的想法吗?

作者:互联网

17.6.2.2标题[using.headers]

3) A translation unit shall include a header only outside of any
external declaration or definition
, and shall include the header
lexically before the first reference in that translation unit to any
of the entities declared in that header.

如果我的解释是正确的:

extern "C"
{
   #include "smth.h"
}

是非法的(那里有很多).

我想念诠释吗? “外部声明”和“外部定义”是否意味着其他东西(在此背景下)?

解决方法:

标准中定义了标头,其中声明或定义了C标准库的元素.这不包括用户的.h文件.见§17.6.1.2:

Each element of the C++ standard library is declared or defined (as appropriate) in a header.

The C++ standard library provides 52 C++ library headers, as shown in Table 14.

The facilities of the C standard Library are provided in 26 additional headers, as shown in Table 15.

所以你给出的例子很好(只要.h文件本身不包含一些标准库头).但是,这不会是:

extern "C"
{
   #include <string>
}

为了进一步备份头文件和源文件(包括.h文件)之间的分离,#include预处理指令被定义为包含带有< h-char-sequence>的“头”.语法并包含“源文件”和“q-char-sequence”语法(第16.2节).所以你的#include“smth.h”包含一个源文件,而不是一个头文件.源文件被定义为程序的文本,它与它包含的标题一起组成一个翻译单元:

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.6.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.

我认为这种措辞的目的是将标准库头与其表示分离.他们没有理由将它们存储为C源文件,即使它们通常也是如此.包含特定标题只需要具有使程序可用的适当声明的效果.

标签:c,language-lawyer,header-files
来源: https://codeday.me/bug/20190831/1779428.html