我应该包括每个标题吗? [英] Should I include every header?

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

问题描述

我应该包括每个标题,即使它以前包括?或者也许我应该避免它,当我可以?
例如。如果我在某些文件中使用 std :: string std :: vector 如果< string> 包括< vector> 应该只包括< string& < string> < vector> ? $ b

解决方案

如果您在文件中使用标题相关实体(例如某种类型),则应包含相关标题。不要依赖标头来包含对方。 如果您使用它,请添加



C ++标准库不要求包含< string& in < vector> ,反之亦然。试图利用这样的功能将代码限制到特定的实现。一般来说,标准库标头可能或可能不会以未指定的顺序或方式包含其他标头(或其自己的内部标头)。一个值得注意的例外是< initializer_list> ,需要包含在几个的其他标准标题



还要考虑如果头文件是类的定义,那么它应该包括该类的定义所需要的。相关的 .cpp 应包含其相关的 .h 以及实现类所需的其余文件。 不需要,不包括;不要包含超过所需的内容( llvm样式指南) )。一个例外是模板(没有关联的 .cpp ;

从长远来看可能是困难的;因此有意义的是,在编码循环的开始包括接口所需的是重要的;然后再次检查包含与对代码做任何合理的更改。



似乎有一些进展w.r.t.在这方面的工具,例如 iwyu项目 ,它使用clang工具链,似乎也支持msvc。



一个计数器示例将是如果标题的原因是包括其他标题,那么也许,但即使这样,我会非常小心 - 确保它清楚地定义它包括什么。一个示例可以是预编译头。


Should I include every header even if it was included before? Or maybe I should avoid it when I can? For example. If I use std::string and std::vector in some file. If <string> included <vector> should I include only <string> or <string> and <vector>?

解决方案

If you use a header related entity (e.g. some type) in a file, you should include the related header for it. Don't rely on headers to include each other. If you use it, include it.

The C++ standard library doesn't mandate inclusion of <string> in <vector> nor vice-versa. Trying to make use of functionality like this would limit the code to a specific implementation. In general, the standard library headers may or may not include other headers (or their own internal headers) in an unspecified order or manner. One notable exception is <initializer_list> which is required to be included in a few of the other standard headers. Changes to this unspecified order or manner can also happen, thus breaking previously compiling code with the updated compiler or an updated standard library implementation (this has been known to happen).

Also consider that if the header file is the definition for the class, then it should include what is required for the definition of that class. The associated .cpp should include its associated .h and the remaining files required to implement the class. Don't need it, don't include it; don't include more than needed (llvm style guide). One exception here is templates (that don't have an associated .cpp); this exception would apply to other header only implementations.

It is noted that maintenance of the include what you use can be difficult in the long run; thus it makes sense that it is important in the beginning of the coding cycle to include what is required for the interface; and then again to check the includes with any reasonable change that is made to the code.

There seems to be some progress w.r.t. tools in this regard, such as the iwyu project, that uses the clang tool chain and seems to have support for msvc as well.

One counter example would be if the reason for the header is to include other headers, then maybe, but even then I would be very careful - make sure it is clearly defined what it includes. An example of this could be a precompiled header.

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

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