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:
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:
I am a beginner programer without experience on big projects and I sincerely want to figure this out so please have mercy upon me.
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.
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.
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.
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: 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.
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.
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.
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?
Besides
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With