Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why not include all the standard headers always?

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.

like image 759
Martin Drozdik Avatar asked May 30 '13 07:05

Martin Drozdik


People also ask

Should headers include other headers?

Implementation. This rule means that if the header uses a type — such as ' FILE * ' or ' size_t ' - then it must ensure that the appropriate other header ( <stdio. h> or <stddef. h> for example) should be included.

Should all Includes be in header file?

A header file should be included only when a forward declaration would not do the job. The header file should be so designed that the order of header file inclusion is not important.

What happens when we include same header multiple times?

If a header file happens to be included twice, the compiler will process its contents twice. This is very likely to cause an error, e.g. when the compiler sees the same structure definition twice. Even if it does not, it will certainly waste time. This construct is commonly known as a wrapper #ifndef.

Why header files inclusion is must?

Including Multiple Header Files: You can use various header files in a program. When a header file is included twice within a program, the compiler processes the contents of that header file twice. This leads to an error in the program. To eliminate this error, conditional preprocessor directives are used.

What doesn't belong in a header?

What doesn't belong in a header: Gratuitous #includedirectives. Those gratuitous includes cause recompilation of things that don't need to be recompiled, and can at times make it so a system can't compile. Don't #includea file in a header if the header itself doesn't need that other header file.

Why shouldn't I include a file in a header?

Those gratuitous includes cause recompilation of things that don't need to be recompiled, and can at times make it so a system can't compile. Don't #include a file in a header if the header itself doesn't need that other header file.

How to avoid the header in C++?

You could (in principle) avoid it entirely by copy & paste, or replace it by another "preprocessor" or C code generator (like gppor m4). An additional issue is that the recent C (or C++) standards define several standard headers.

What should be included in the header of a header?

The prototype in the header should summarize, in some extractable comment (e.g., doxygen), what the arguments are and what the function does. Why does this data member exist, what does it contain, and is the value in meters, feet, or furlongs?


1 Answers

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.

like image 175
Alex Avatar answered Oct 21 '22 01:10

Alex