Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any guaranteed dependency of C++ system headers?

Tags:

c++

For example, in <algorithm>, the function equal_range returns a pair, so can I assume that if I #include <algorithm>, <utility> is #included?

like image 283
Qian Avatar asked Jun 29 '11 10:06

Qian


People also ask

Does every C file need a header?

Header files are not mandatory. Commonly used in real life projects are global header files like config. h and constants. h that contains commonly used information such as compile-time flags and project wide constants.

Does order of header files matter in C?

Does the include order matter? If your header files are self-contained, then the include order technically shouldn't matter at all for the compilation result.

How many C header files are there?

There are 19 header files in the Standard C Library.

Can C program run without header file?

Yes you can wirte a program without #include , but it will increase the complexity of the programmer means user have to write down all the functions manually he want to use.It takes a lot of time and careful attention while write long programs.


2 Answers

There is never a guaranteed inclusion of header files that other headers depend on. Fortunately, it's common practice (though not certain 100% of the time) that headers are guarded against multiple inclusion -- meaning you can #include them as many times as you wish without causing harm.

like image 152
mah Avatar answered Oct 01 '22 08:10

mah


You should always include what you need, you cannot rely on all implementations including the same set of headers in another header.

In your example, if a function returns a pair you can be reasonably sure that the pair class is declared, but nothing requires the implementation to include the rest of <utility>.

In fact, it is impossible to implement the standard library using exactly the headers shown in the standard, because there are some circular references. Implementations must split them in smaller parts, and include those sub-headers in the required <> headers.

The GCC team, for example, is working to minimize the amount of inclusions to speed up compile time.

like image 21
Bo Persson Avatar answered Oct 01 '22 09:10

Bo Persson