21

As the book says (Exploring C++: The Programmer's Introduction to C++):

The istream header declares input operators (>>), and ostream declares output operators(<<).

I can perfectly run the code without adding #include <ostream>:

#include <iostream>
using namespace std;
int main()
{
    cout << "hello world"<< endl;
    return 0;
}

But, in book's example like:

#include <iostream>
#include <ostream>    //why?
using namespace std;
int main()
{
    cout << "hello world"<< endl;
    return 0;
}

So, iostream, ostream and istream are header files right?

If ostream is not necessary (iostream does the job) why does the author include it in the example? Or why does the ostream header file still exist?

Note: In Bruce Eckel's Vol 1 book (which is published in 2000) there is nothing about ostream or istream. Only one header file which is iostream.

Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
Mustafa Ekici
  • 7,263
  • 9
  • 55
  • 75
  • It is a good picture, shows relations between streams: http://www.cplusplus.com/reference/iostream/. Athor may had forgotten to remove header, or it uses it later. – Lol4t0 Feb 13 '12 at 19:50
  • 9
    @Lol4t0, hell never link anyone to cplusplus.com. It's one of the worst websites about C++. Use CppReference instead. – Griwes Feb 13 '12 at 20:05
  • 2
    Google's not a programmer's reference, you know. – Griwes Feb 13 '12 at 20:08
  • The accepted answer is incorrect; please consider accepting a highly-upvoted answer instead – M.M Mar 17 '16 at 05:08
  • @Griwes What about now? Is it still the worst website in 2023? – iTzVoko Jun 09 '23 at 00:56

5 Answers5

20

As ildjarn noted in the comment, C++ standard from 2003 says that iostream does not necessary include istream and ostream. So, theoretically, the book is correct.

However, most major compiler vendors have added istream and ostream into iostream, so your code works on the compiler you are using. You might not have such luck on some other compiler.

If you want to write portable code that would compile on older compilers that only adhere to 2003 standard (or earlier), you should include both headers. OTOH, if you are the only one compiling your code and have control which compilers would be used, it's safe to use iostream only, because that is forward-compatible.

Milan Babuškov
  • 59,775
  • 49
  • 126
  • 179
  • 10
    "*Note that iostream includes both istream and ostream*" C++03 does not guarantee this, only C++11; prior to C++11, one must `#include ` in order to have guaranteed inclusion of `std::endl`. – ildjarn Feb 13 '12 at 19:52
  • so they call about standard library, where is standard of that? – Mustafa Ekici Feb 13 '12 at 19:53
  • 2
    *"some compilers (GCC 3.x for example) include iostream automatically when compiling C++ code"* -- are you sure about that? gcc 3.4.6 doesn't do that, and I wouldn't expect it from any version of gcc. – Keith Thompson Feb 13 '12 at 20:02
  • 1
    @mekici : The _official_ standard is not freely available, but [N3337](http://www.open-std.org/JTC1/SC22/WG21/docs/papers/2012/n3337.pdf) should suffice as a substitute for the C++11 standard (I don't know of good/free substitutes for the C++03 or C++98 standards). – ildjarn Feb 13 '12 at 20:03
  • 4
    "*C++ standard from 2003 (still the official one)*" The C++11 standard is official, it's just not what the book the OP is reading targets. – ildjarn Feb 13 '12 at 20:08
  • 7
    A standard library implementation that requires explicitly including `` to get HelloWorld to work is a lot like Francium, the most unstable naturally occurring element (http://en.wikipedia.org/wiki/Francium). Both of them have a half-life of about 22 minutes. In the case of the std::library implementation, this refers to the amount of time it would survive market forces before being abandoned. – Howard Hinnant Feb 14 '12 at 01:14
  • 1
    @Howard : That would imply that HelloWorld requires `std::endl`, when it most certainly doesn't. ;-] – ildjarn Feb 14 '12 at 02:20
  • 1
    @KeithThompson: I could have sworn, but looks like you're right. I fixed the answer. – Milan Babuškov Feb 14 '12 at 16:03
  • @MilanBabuškov I dont understand one thing why the author didnt tell that as you say? "If you want to write portable code that would compile on older compilers that only adhere to 2003 standard (or earlier), you should include both headers." For example Bruce Eckels's firt book it published in 2000 but in his examples he only use iostream not ostream and istream – Mustafa Ekici Feb 16 '12 at 00:06
  • @mekici : He was being lazy and relying on the standard library implementation to do the extra includes for you, which was not guaranteed to happen. Books are not authoritative; the standard is, and the standard (pre-C++11) made no guarantees here. – ildjarn Feb 16 '12 at 00:07
  • @ildjarn so what about bruce eckel's book examples? – Mustafa Ekici Feb 16 '12 at 00:14
  • @mekici : Eckel is who I was referring to with "he"; his books' examples will work on most pre-C++11 implementations, but are not _guaranteed_ to. I really don't understand what is so confusing here. – ildjarn Feb 16 '12 at 00:16
  • @ildjarn this book was written in 2000 before 2003 standard he had to include his examples ostream but he only used iostream header – Mustafa Ekici Feb 16 '12 at 00:19
  • @mekici : C++98 and C++03 are both pre-C++11. _Only_ C++11 makes the guarantee that including `iostream` will also include `istream` and `ostream`. – ildjarn Feb 16 '12 at 00:20
  • @ildjarn ohh! ok c++03 is standartizated in 2003 right? so they said ok you dont need to include ostream, so if eckel's book written in 2000 (in that time standard was c++ 98=so we can use iostream without ostream) so after 2003 ostream borned? I mean if c++98 doesnt guarantee that why eckel's examples doesnt include ostream? – Mustafa Ekici Feb 16 '12 at 00:31
  • @mekici : C++98 came first (for ISO standards), then C++03, then C++11; _only_ C++11 says that including `iostream` is also guaranteed to include `istream` and `ostream`. Eckel's books are "wrong" in that they use things from these headers without including them, even though it usually works in practice. Again, I really can't fathom what is confusing about this. – ildjarn Feb 16 '12 at 00:34
  • @ildjarn I just want to understand! I dont care it includes or not! btw if you say eckel's books are wrong, I dont have much things to say :) – Mustafa Ekici Feb 16 '12 at 00:37
  • @mekici : Yes, the books are _technically_ wrong, but so much other code is also wrong in the same way that for C++11 they decided to guarantee that it would work instead of it being implementation-defined. – ildjarn Feb 16 '12 at 00:39
  • Eckel's and other books are theoretically wrong, but most compilers at the time already included everything in iostream. My guess is that Eckel tested his code in mainstream compilers at the time (MSVC, GCC, Borland, and maybe some other) and it worked everywhere. – Milan Babuškov Feb 16 '12 at 12:48
12

In C++11, as specified by the standard in §27.4.1, the header iostream includes the istream and ostream headers in itself, so the #include <ostream> is redundant.

The 'synopsis' of iostream given by the standard in the aforementioned section is

#include <ios>
#include <streambuf>
#include <istream>
#include <ostream>

namespace std {
    extern istream cin;
    extern ostream cout;
    extern ostream cerr;
    extern ostream clog;

    extern wistream wcin;
    extern wostream wcout;
    extern wostream wcerr;
    extern wostream wclog;
}
Seth Carnegie
  • 73,875
  • 22
  • 181
  • 249
  • 5
    C++03 does not guarantee this, only C++11; prior to C++11, one must `#include ` in order to have guaranteed inclusion of `std::endl`. – ildjarn Feb 13 '12 at 19:52
  • 1
    @SethCarnegie thanks for answer btw can you tell me what is §27.4.1? I saw it in a book but i dont get it? what is § ? – Mustafa Ekici Feb 13 '12 at 20:01
  • 4
    @mekici § is the symbol for the word "section" and it means that the information I am telling you came from section 27.4.1 in the C++11 Standard (which, in case you don't know, is the document that defines everything about the C++ language) – Seth Carnegie Feb 13 '12 at 20:03
6

You need #include <iostream> to get access to the standard stream objects such as cout. The author of that code is making sure to not rely on the implementation detail that <iostream> includes <ostream> (that wasn't guaranteed prior to C++11).

You need <ostream> to get access to operator << and std::endl.

Michael Kristofik
  • 34,290
  • 15
  • 75
  • 125
4

So After that; I sent email to Bjarne Stroustrup and He replied just like that:
Personally, I always use iostream and you never need both. ostream exists so that people can #include only the minimaldeclaration needed.

enter image description here

Mustafa Ekici
  • 7,263
  • 9
  • 55
  • 75
3

I can perfectly run that code without adding #include ostream;

You happen to be able to on your specific installation. Upgrade your toolchain and that may no longer be the case.

As of C++11 you can assume it for iostream/ostream, but there are other similar scenarios which C++11 does not cover.

So, a general rule of thumb: whenever you use a standard library feature, include the header where it's declared/defined, rather than making assumptions and shortcuts.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055