Mustafa Ekici
Mustafa Ekici

Reputation: 7470

iostream vs ostream what is different?

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.

Upvotes: 21

Views: 21693

Answers (5)

Lightness Races in Orbit
Lightness Races in Orbit

Reputation: 385194

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.

Upvotes: 4

Mustafa Ekici
Mustafa Ekici

Reputation: 7470

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

Upvotes: 5

Milan Babuškov
Milan Babuškov

Reputation: 61138

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.

Upvotes: 20

Seth Carnegie
Seth Carnegie

Reputation: 75130

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;
}

Upvotes: 12

Michael Kristofik
Michael Kristofik

Reputation: 35188

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.

Upvotes: 6

Related Questions