我应该包含一个已经通过其他标题包含的标题吗? [英] should I include a header that is already included via other headers?

查看:24
本文介绍了我应该包含一个已经通过其他标题包含的标题吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚注意到我的使用字符串类的程序在编译时没有包含 <string> 标头.事实证明,<iostream> 包含 <ios_base>,而 <string> 又包含 <string>.

I had only just noticed my programs using the string class were compiling without including the <string> header. It turns out that <iostream> includes <ios_base> which in turn includes <string>.

这是不好的做法,我应该明确包含 <string> 吗?哪怕只是为了澄清?

Is this bad practice and should I explicitly include <string>? Even if it's just a case of clarity?

假设这不仅仅适用于 <string> 标头是否安全?也许这是特定于实现的,或者标准状态 <string> 标头是否通过 <ios_base><iostream> 包含在内?确保任何受人尊敬且广泛使用的实现将始终包含 <string> 提供对 <iostream> 的调用存在.

Is it safe to assume this applies to more than just the <string> header? Perhaps this is implementation specific and or does the standard state the <string> header be included via <ios_base> and <iostream>? Ensuring that any respected and widely used implementation will always include <string> providing the the call to <iostream> exists.

推荐答案

你应该明确地包含你需要的任何标准库头文件.

You should explicitly include whatever standard library headers you need.

没有指定其他标准库头文件包含哪些标准库头文件,因此这些细节在编译器之间会有所不同.

It is not specified which standard library headers are included by other standard library headers, so such details will differ between compilers.

可以依赖一个标题包含在另一个标题中的一种情况是,一个标题中的类派生自另一个标题中的类.例如, 必须包含 因为在 中定义的类派生自 中定义的类代码><ios_base>.

One case where you can rely on a header being included by another header is if a class in one header derives from a class in another. For example, <iostream> has to include <ios_base> because classes defined in <iostream> are derived from classes defined in <ios_base>.

这篇关于我应该包含一个已经通过其他标题包含的标题吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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