是否不确定标准库头是否包含任意头? [英] Is it unspecified whether a standard library header includes arbitrary headers?

查看:94
本文介绍了是否不确定标准库头是否包含任意头?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

C ++标准库头可能在未指定的情况下互相包括 方式,所以程序员通常不应该依赖一个标头 包括另一个. [...]

The C++ standard library headers may include each other in unspecified ways, so programmers generally shouldn't depend on one header including another. [...]

在实践中,情况往往如此.例如,<iostream>可能包含<string>,在其他情况下,您需要显式包含<string>.但是,我似乎找不到在N4140中的情况.我看过:

In practice this tends to be the case. For example, <iostream> may include <string>, in other cases you need to include <string> explicitly. However, I can't seem to find where in N4140 this is the case. I've looked in:

  • §2.9[lex.header]
  • §17.6.1.2[标题]
  • §17.6.2.2[using.headers]
  • §17.6.4.4[alt.headers]
  • §17.6.5.2[页首标题]

我能找到的最接近的是[using.headers]:

The closest I can find is from [using.headers]:

2翻译单元可以按任何顺序包括库头(条款 2).每个都可以被多次包含,其作用与 只被包含一次,除了包含的效果 <cassert><assert.h>每次都取决于词法 NDEBUG 的当前定义. 178

2 A translation unit may include library headers in any order (Clause 2). Each may be included more than once, with no effect different from being included exactly once, except that the effect of including either <cassert> or <assert.h> depends each time on the lexically current definition of NDEBUG.178

但这似乎适用于C ++程序,而不适用于标准库:

But this seems to apply to C++ programs, not the standard library:

[using.overview]/1本节介绍C ++程序如何获得访问权限 C ++标准库的功能. [...]

[using.overview]/1 This section describes how a C++ program gains access to the facilities of the C++ standard library. [...]

以及[res.on.headers]:

As well as [res.on.headers]:

1 C ++标头可能包含其他C ++标头. C ++标头应 提供出现在提要中的声明和定义. 概要中显示的C ++标头包括其他C ++标头 应提供出现在 其他标头的摘要.

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.

我认为关键是第一句话,但并未明确指出这是未指明的行为.它是否在任何地方都指出这是未指定的行为,或者只是暗示了什么?

I think the key is the first sentence, but it doesn't explicitly say that it's unspecified behavior. Does it state anywhere that this is unspecified behavior or is it simply implied?

推荐答案

鉴于[res.on.headers]是在C ++ 14标准下的

Given that [res.on.headers] is found in the C++14 standard under

17图书馆简介

17.6全库要求

17.6.5符合标准的实现

17.6.5.2标头

17 Library Introduction

17.6 Library-wide requirements

17.6.5 Conforming implementations

17.6.5.2 Headers

可以肯定地说,本段中的"C ++头"表示C ++标准库头.该术语应理解为与C标头(即从C标准库中学习到的那些标头)相对,C标头在这方面的自由度较低. [res.on.headers]对他们说:

it seems safe to say that "C++ headers" in this paragraph means C++ standard library headers. The term is to be understood as opposed to C headers (i.e., those cargo-culted from the C standard library), which are less free in this regard. [res.on.headers] says about them:

C标准标头(D.5)应仅包括其相应的C ++标准标头,如17.6.1.2.中所述.

The C standard headers (D.5) shall include only their corresponding C++ standard header, as described in 17.6.1.2.

由于C ++标准库头文件可能包括其他C ++标准库头文件,因此在包含另一个标准库头文件后,其他任何标准库头文件的内容都可能会为人所知,但除非声明标题为必须包含另一个标头,这是不可靠的.一个标题必须是否包含另一个标题是在逐个标题的基础上定义的.例如,对于<iostream>,它在[iostream.objects.overview](27.4.1)中说:

Since a C++ standard library header may include other C++ standard library headers, the contents of any other standard library header may become known after including another, but unless it is stated that a header must include another header, this is not reliable. Whether one header must include another is defined on a header-by-header basis. For example, for <iostream> it says in [iostream.objects.overview] (27.4.1):

标题<iostream>简介

Header <iostream> synopsis

#include <ios>
#include <streambuf>
#include <istream>
#include <ostream>

(...)

在[template.bitset](20.7)中

And in [template.bitset] (20.7)

标题<bitset>简介

Header <bitset> synopsis

#include <string>
#include <iosfwd>        // for istream, ostream

(...)

这篇关于是否不确定标准库头是否包含任意头?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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