should I include a header that is already included via other headers?(我应该包含一个已经通过其他标题包含的标题吗?)
问题描述
我刚刚注意到我的使用字符串类的程序在编译时没有包含 <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>.
这篇关于我应该包含一个已经通过其他标题包含的标题吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:我应该包含一个已经通过其他标题包含的标题吗?
基础教程推荐
- 通过引用传递 C++ 迭代器有什么问题? 2022-01-01
- 非静态 const 成员,不能使用默认赋值运算符 2022-10-09
- 为什么 typeid.name() 使用 GCC 返回奇怪的字符以及如 2022-09-16
- 我应该对 C++ 中的成员变量和函数参数使用相同的名称吗? 2021-01-01
- 为什么派生模板类不能访问基模板类的标识符? 2021-01-01
- CString 到 char* 2021-01-01
- 初始化列表*参数*评估顺序 2021-01-01
- 如果我为无符号变量分配负值会发生什么? 2022-01-01
- GDB 显示调用堆栈上函数地址的当前编译二进制文 2022-09-05
- 为什么 RegOpenKeyEx() 在 Vista 64 位上返回错误代码 2021-01-01
