标准是否指定 headers 包括其他 headers?
Does the standard specify what headers include other headers?
我正在参加在线编码竞赛,我的想法是找到一个 header,它的名称比 <iostream>
短,但包含 <iostream>
。好吧,直到现在我才成功,但这让我想知道:
标准是否指定了哪些 header 包括其他 header?
例如,在 <iostream>
cplusplus 状态:
Including this header may automatically include other headers, such as <ios>
, <streambuf>
, <istream>
, <ostream>
and/or <iosfwd>
.
然而,当我查找<ios>
时,没有"This header might be included by <iostream>
"这样的语句。对于一些 headers 我可以想象他们需要包括其他人才能正常工作。如果是这种情况,我希望标准能够说明 header 是如何相互依赖的(例如,必须避免循环依赖)。或者标准是否只是确保不存在此类依赖关系并且取决于实现?
据 我 所知,该标准没有指定 headers 包括其他 headers - 这是实现定义的,而不是你应该依赖的东西在。包括您使用的内容。
此外,您可能希望 http://cppreference.com/ over http://cplusplus.com/ 质量更高(恕我直言)。
总的来说,没有,标准没有规定header必须包含其他header;在需要时管理从其他 header 获取名称取决于实现,并且允许标准 header 包含任何其他标准 header。
唯一的例外是 header <initializer_list>
; header 使用初始值设定项列表(例如容器)的 header 需要包含 header。
关于 headers 可能被其他 headers 包含的一般规则是:有一些 headers 被指定为自动包含,除此之外,它取决于实现,不需要记录。
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++ headers”的规则适用于:
<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],这很好。在大多数情况下,实现是否包含其他 headers 没有任何区别,只要声明的函数和类型可访问即可。
这取决于指的是哪个标准版本。 C++98 和 C++03 对 header 没有要求,包括其他 header。
从 C++11 或 C++14 开始,标准 确实 强制某些 header 包含其他 header。
标准的所有版本都要求多次包含相同的 header 是安全的。
因此,如果您知道自己需要特定的 header,并且您正在使用实现旧标准的编译器,只需 #include header.
我正在参加在线编码竞赛,我的想法是找到一个 header,它的名称比 <iostream>
短,但包含 <iostream>
。好吧,直到现在我才成功,但这让我想知道:
标准是否指定了哪些 header 包括其他 header?
例如,在 <iostream>
cplusplus 状态:
Including this header may automatically include other headers, such as
<ios>
,<streambuf>
,<istream>
,<ostream>
and/or<iosfwd>
.
然而,当我查找<ios>
时,没有"This header might be included by <iostream>
"这样的语句。对于一些 headers 我可以想象他们需要包括其他人才能正常工作。如果是这种情况,我希望标准能够说明 header 是如何相互依赖的(例如,必须避免循环依赖)。或者标准是否只是确保不存在此类依赖关系并且取决于实现?
据 我 所知,该标准没有指定 headers 包括其他 headers - 这是实现定义的,而不是你应该依赖的东西在。包括您使用的内容。
此外,您可能希望 http://cppreference.com/ over http://cplusplus.com/ 质量更高(恕我直言)。
总的来说,没有,标准没有规定header必须包含其他header;在需要时管理从其他 header 获取名称取决于实现,并且允许标准 header 包含任何其他标准 header。
唯一的例外是 header <initializer_list>
; header 使用初始值设定项列表(例如容器)的 header 需要包含 header。
关于 headers 可能被其他 headers 包含的一般规则是:有一些 headers 被指定为自动包含,除此之外,它取决于实现,不需要记录。
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++ headers”的规则适用于:
<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],这很好。在大多数情况下,实现是否包含其他 headers 没有任何区别,只要声明的函数和类型可访问即可。
这取决于指的是哪个标准版本。 C++98 和 C++03 对 header 没有要求,包括其他 header。
从 C++11 或 C++14 开始,标准 确实 强制某些 header 包含其他 header。
标准的所有版本都要求多次包含相同的 header 是安全的。
因此,如果您知道自己需要特定的 header,并且您正在使用实现旧标准的编译器,只需 #include header.