头是否必须是文件? [英] Is a header necessarily a file?

查看:74
本文介绍了头是否必须是文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

T.C.对此问题的回答留下了有趣的评论:



为什么不包括c ++中的保护默认值?



TC状态:


有header,并有源文件。 header不需要是
个实际文件。


这是什么意思? b
$ b

通过标准,我看到大量的引用头文件和标题。但是,关于 #include ,我注意到标准似乎引用headers和source files 。 (C ++ 11,§16.2)

  
形式的预处理指令include# h-char-序列>换行
在一系列实现定义的位置中搜索由
唯一标识的头,和>分隔符,并导致该指令的替换
的头部的整个内容。如何指定位置
或标识的头是实现定义的。

  
形式的预处理指令$#包含q-char-sequence新行
导致该指令被源*文件的全部内容替换*
通过在定界符之间的指定顺序识别。以实现定义的方式搜索命名的源*文件*

我不知道这是否是重要的。可能是C ++上下文中的headers明确意味着头文件,但是sources一词将是歧义的,因此headers一个简写但是sources不是,或者可能是一个C ++编译器允许留有余地的括号包括,只需要像文本替换发生。





在下面的注释中,TC提到的脚注是非常直接的:


174)标题不一定是源文件,序列
也不是由<



解决方案

对于标准头文件,C ++标准并不真正要求编译器使用一个文件,或者文件,如果它使用一个,实际上看起来像一个C ++文件。而是,指定标准头文件以使得一组声明和定义可用于C ++程序。



文件的替代实现可以是容易打包的集合在编译器中表示为数据结构的声明在使用相应的 #include -directive时可用。我不知道任何编译器这样做,但 clang 开始实现模块系统,可以使用某些已处理格式的标头。


T.C. left an interesting comment to my answer on this question:

Why aren't include guards in c++ the default?

T.C. states:

There's "header" and there's "source file". "header"s don't need to be actual files.

What does this mean?

Perusing the standard, I see plenty of references to both "header files" and "headers". However, regarding #include, I noticed that the standard seems to make reference to "headers" and "source files". (C++11, § 16.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.

and

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.

I don't know if this is significant. It could be that "headers" in a C++ context unambiguously means "header files" but the word "sources" would be ambiguous so "headers" is a shorthand but "sources" is not. Or it could be that a C++ compiler is allowed leeway for bracket includes and only needs to act as if textual replacement takes place.

So when are header (files) not files?

The footnote mentioned by T.C. in the comments below is quite direct:

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

解决方案

For the standard header "files" the C++ standard doesn't really make a mandate that the compiler uses a file or that the file, if it uses one, actually looks like a C++ file. Instead, the standard header files are specified to make a certain set of declarations and definitions available to the C++ program.

An alternative implementation to a file could be a readily packaged set of declarations represented in the compiler as data structure which is made available when using the corresponding #include-directive. I'm not aware of any compiler which does exactly that but clang started to implement a module system which makes the headers available from some already processed format.

这篇关于头是否必须是文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆