我正在参加在线编码竞赛,我的想法是找到一个名称比 <iostream>
短的 header 但包括 <iostream>
.好吧,直到现在我才成功,但这让我想知道:
标准是否指定哪些 header 包含其他 header ?
例如,在 <iostream>
上cplusplus状态:
Including this header may automatically include other headers, such as
<ios>
,<streambuf>
,<istream>
,<ostream>
and/or<iosfwd>
.
但是,当我寻找 <ios>
没有诸如“此 header 可能包含在 <iostream>
中”之类的声明。对于某些 header ,我可以想象它们需要包含其他 header 才能正常工作。如果是这种情况,我希望标准能够说明 header 如何相互依赖(例如,必须避免循环依赖)。还是标准只是确保不存在此类依赖关系,这取决于实现?
最佳答案
关于哪些 header 可以被其他 header 包含的一般规则是:有一些 header 被指定为自动包含,除此之外,它取决于实现,不需要记录。
17.6.5.1 Conforming implementations [conforming]
17.6.5.2 Headers [res.on.headers]
1 A C++ header may include other C++ headers. A C++ header shall provide the declarations and definitions that appear in its synopsis. A C++ header shown in its synopsis as including other C++ headers shall provide the declarations and definitions that appear in the synopses of those other headers.
关于“在其概要中显示为包含其他 C++ header ”的规则适用于:
-
<utility>
,<string>
,<array>
,<deque>
,<forward_list>
,<list>
,<vector>
,<map>
,<set>
,<unordered_map>
,<unordered_set>
,<queue>
,<stack>
,<algorithm>
,<random>
,<valarray>
,<regex>
包括<initializer_list>
. -
<bitset>
包括<string>
,<iosfwd>
. -
<iostream>
包括<ios>
,<streambuf>
,<istream>
,<ostream>
. -
<ios>
包括<iosfwd>
.
然而,尽管例如<ios>
显示为包括 <iosfwd>
,实现可能能够在不实际包含它的情况下实现要求,并且根据 [res.on.headers],这很好。在大多数情况下,实现是否包含其他 header 没有任何区别,只要声明的函数和类型可访问即可。
关于c++ - 该标准是否指定哪些 header 包含其他 header ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38421334/