Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should I include every header?

Tags:

c++

c++11

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>?

like image 683
peter55555 Avatar asked Oct 28 '14 14:10

peter55555


People also ask

Should you put all includes in header file?

Generally, you only want to put the minimum necessary includes into a class header file, as anyone else who uses that header will be forced to #include all of them too.

Should headers include other headers?

This standard requires a unit's header to contain #include statements for all other headers required by the unit header. Placing #include for the unit header first in the unit body allows the compiler to verify that the header contains all required #include statements.

Are header files necessary?

Yes, because it's still based on C. You can answer your own question: Don't use them and try to compile without them. If you can't, then the compilers still require them.

What should be included in headers?

Headers and footers generally contain additional information such as page numbers, dates, an author's name, and footnotes, which can help keep longer documents organized and make them easier to read. Text entered in the header or footer will appear on each page of the document.


1 Answers

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.

like image 95
Niall Avatar answered Oct 14 '22 12:10

Niall