Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which C++ header includes which other headers?

I am confused of which standard library include which other once. I heard that iostream includes ostream or something like that. Unfortunately I couldn't find an overview. That's why I ask now.

My program uses the following libraries: string, fstream, iostream, sstream, streambuf. Because they provide related functionality I wonder if any of these includes another of them already. In this case I could get rid of the redundant includes.

Is there a overview out there indicating which standard library includes which? or Which of the libraries user by my program are redundant?

like image 443
danijar Avatar asked Dec 15 '22 18:12

danijar


2 Answers

C++ provides no guarantees in general for any sort of recursive inclusion. It is your responsibility to always include all headers that you need. Similarly, it does not guarantee that any particular standard library header is not included. (For example, your implementation could legally always include all standard library headers!) That's why-and-because everything is in the std namespace.

(I believe there's a special provision for the C library headers - I think you won't get names in the global namespace unless you explicitly include those headers.)

Some headers do have specific requirements; for example, in C++11 (but not before) it is required that <iostream> include both <ostream> and <istream>. But that's just one specific case.

like image 123
Kerrek SB Avatar answered Dec 28 '22 08:12

Kerrek SB


You can't rely on any headers being included by any other headers. It's best to explicitly include everything you need. That way, if you change the compiler, you can be sure not to break compilation, in case the new compiler doesn't have the same header include structure.

like image 24
Luchian Grigore Avatar answered Dec 28 '22 08:12

Luchian Grigore