Reputation: 34185
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?
Upvotes: 5
Views: 258
Reputation: 718
If you are worried of retundant includes they are handled via macros like
#ifndef MACRO_NAME #define MACRO_NAME
#endif
So you dont need to worry about multiple includes i guess.
Upvotes: 1
Reputation: 33126
My rule: #include
what you are using. If you are using std::string
, you need to #include <string>
. If you are using std::cout
, you need to #include <iostream>
. If you are using a file stream, #include <fstream>
. And so on.
That you might get std::string
for "free" from some of those other system headers that you include is irrelevant. Don't depend on those behind the scenes includes.
Upvotes: 1
Reputation: 258618
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.
Upvotes: 2
Reputation: 477120
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.
Upvotes: 8
Reputation: 19
This may help: http://www.cplusplus.com/reference/iostream/iostream/ iostream is inherited from ostream and istream.
Upvotes: 1