为什么不总是包括所有的标准标题? [英] Why not include all the standard headers always?

查看:144
本文介绍了为什么不总是包括所有的标准标题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读Herb Sutter的更多例外的C ++ 项目37的前进声明说:


从前 #include 一个标题,当一个向前声明就足够了。
当完整定义
流时,优先于 #include 只有< iosfwd>


此外,我听到了大量的建议,只包括编译单元需要的头文件来减少依赖。



我理解为什么这应该适用于项目标题,但我不太明白为什么包括不必要的标准标头。



例如我做了这样的事情:

  // standard_library .h 

#ifndef STANDARD_LIBRARY
#define STANDARD_LIBRARY

#include< iostream>
#include< chrono>
#include< thread>
...
//项目中需要的一切
#endif


$ b b

并且包括这个单一的标头到哪里,我需要 std



想象是:


  1. 不需要在std命名空间中的C库函数对命名空间造成污染。

  2. 编译时间较慢

但我没有遇到过1. sofar的问题。几乎所有的都在std命名空间。我也不完全明白为什么2.必然是一个重大问题。标准标题很少更改。也就是说,我知道编译器可以预编译它们。当涉及到模板时,只有当我需要它们时,它们才被实例化(编译)。



还有好处:


  1. 减少键入

  2. 减少阅读次数

  3. 了解我需要哪些标题,是

我是一个初级程序员,没有大项目的经验,我真诚地想要这样做,请怜悯我。 / p>

除了




  • / li>
  • 编译时间(虽然可以通过预编译头来减少,但是它会伤害编译大型项目一次,因为他们实际上想要使用它,而不是开发 - 也想考虑重建是必要的



您的名字是少了解我需要哪些标头和某个功能在哪个标头 。我同意,因为这对于精心设计的库和头文件可以是真的。



在实践中,我经历了(至少与MFC / ATL)一些错误,可以解决通过找出包括的正确顺序。另一方面,有一天你想解决一个问题,让你穿越包含的标题 - 现在想象你自己看看大量的头文件实际上与你的代码文件无关。



我的结论是:通过包含一堆不必要的标头保存的时间不会得到回报,如果你必须在以后维护一个大项目。在开始包含任何标题之前,您投入的时间越多,您在之后的安全时间就越多 - 但大多数情况下没有实际识别。


I am reading Herb Sutter's More Exceptional C++ and item 37 on forward declarations says:

Never #include a header when a forward declaration will suffice. Prefer to #include only <iosfwd> when the complete definition of a stream is not needed.

Also I heard plenty of advice on including only the headers needed by the compilation unit to reduce dependencies.

I understand perfectly well why this should apply to project headers, but I do not quite understand why is it bad to include unnecessary standard headers.

For example I do something like this:

//standard_library.h

#ifndef STANDARD_LIBRARY
#define STANDARD_LIBRARY

#include <iostream>
#include <chrono>
#include <thread>
...
// Everything I need in the project
#endif

and include this single header everywhere, where I need something from std

The problems that I can imagine are:

  1. Pollution of namespace by C library functions that do not need to be in the std namespace.
  2. Slower compilation time

But I haven't had significant problems with 1. sofar. Almost everything is in the std namespace. Also I do not fully understand why 2. is necessarily a significant problem. The standard headers rarely change. Also as far as I know the compiler can precompile them. When it comes to templates, they are instantiated(compiled) only when I need them.

There are also benefits:

  1. Less typing
  2. Less reading
  3. Less figuring out which headers I need and in which header a certain function is

I am a beginner programer without experience on big projects and I sincerely want to figure this out so please have mercy upon me.

解决方案

Besides

  • namespace pollution
  • compilation time (although reducable by precompiled headers, it will hurt those compiling a large project once because they actually want to use it, and not develop - also you want to think about rebuilds which are necessary once in a while)

you named "Less figuring out which headers I need and in which header a certain function is" as a benefit. I agree so far as this can be true for well designed libraries and headers.

In practice however I experienced (at least with MFC/ATL) some errors which could be solved by figuring out the correct order of includes. On the other hand one day you want to resolve an issue which makes you travel across the included headers - now imagine yourself looking at tons of headerfiles actually having nothing to do with your code file.

My conclusion is: The time you save by including a bunch of unnecessary headers do not pay off if you have to maintain a large project later on. The more time you invest before starting including any headers, the more time you will safe afterwards - but mostly without actually recognizing it.

这篇关于为什么不总是包括所有的标准标题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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