which headers to #include - std::ostream std::iostream std::iosfwd

S

subramanian100in

Suppose I have a program which uses 'ostream' type as the parameter
type for a function and 'cout' object for writing into the standard
output. Following is my understanding regarding, which headers to
#include for this scenario.

Since the program uses 'cout' object, I have to #include <iostream>
header for 'cout'. In the ISO/IEC-14882:2003 document, in page 608, in
section 27.3 - Standard iostream objects, the synopsis for the header
<iostream> mentions the following line(apart from other statements):
extern ostream cout;
From the above statement, since 'cout' is mentioned for <iostream>,
#including <iostream> is sufficient for using 'cout' object in my
program. Moreover, since 'ostream' is also mentioned in the same
statement, the forward declaraion of 'ostream' type should also be
known in the <iostream> header. This means that I need NOT #include
<iosfwd>. Is this
correct ? Since the class definition of the type 'ostream' is
needed(because the function parameter type is 'ostream&'), I HAVE to
#include <ostream> header instead of relying on the particular
compiler implementation #including the <ostream> inside <iostream>.
So, for the scenario mentioned in the beginning, I need ONLY to
#include
#include <iostream>
#include <ostream>
// ...
using namespace std;

I have NOT #included <iosfwd> header.

Is the above understanding of mine is correct ?
Correct me wherever I am wrong.

Kindly explain.

Thanks
V.Subramanian
 
S

Saeed Amrollahi

Suppose I have a program which uses 'ostream' type as the parameter
type for a function and 'cout' object for writing into the standard
output. Following is my understanding regarding, which headers to
#include for this scenario.

Since the program uses 'cout' object, I have to #include <iostream>
header for 'cout'. In the ISO/IEC-14882:2003 document, in page 608, in
section 27.3 - Standard iostream objects, the synopsis for the header
<iostream> mentions the following line(apart from other statements):
     extern ostream cout;
From the above statement, since 'cout' is mentioned for <iostream>,
#including <iostream> is sufficient for using 'cout' object in my
program. Moreover, since 'ostream' is also mentioned in the same
statement, the forward declaraion of 'ostream' type should also be
known in the <iostream> header. This means that I need NOT #include
<iosfwd>. Is this
correct ? Since the class definition of the type 'ostream' is
needed(because the function parameter type is 'ostream&'), I HAVE to
#include <ostream> header instead of relying on the particular
compiler implementation #including the <ostream> inside <iostream>.
So, for the scenario mentioned in the beginning, I need ONLY to
#include
    #include <iostream>
    #include <ostream>
    // ...
    using namespace std;

I have NOT #included <iosfwd> header.

Is the above understanding of mine is correct ?
Correct me wherever I am wrong.

Kindly explain.

Thanks
V.Subramanian

Hi
I think your understanding in general is correct.
If you want to use the standard narrow and wide
iostream objects you have to include <iostream>.
It's the reason, why the the typical "hello, world" program
is like this:

#include <iostream>
int main()
{
using namespace std;
cout << "Hello, world\n";
return 0;
}

If you want use just output stream facilities, you can
include just <ostream>. Similarly, if you
want use just output stream facilities, you can
include just <istream>.
consider:

#include <istream>
struct istream_wrapper
{
istream_wrapper(std::istream& is_) : is(is_) {}
std::istream& is;
};

#include <ostream>
struct ostream_wrapper
{
ostream_wrapper(std::eek:stream& os_) : os(os_) {}
std::eek:stream& os;
};

#include <iostream> // you have to include this
int main()
{
using namespace std;
istream_wrapper iw(cin);
ostream_wrapper ow(cout);
int i;
iw.is >> i;
ow.os << i << '\n';

return 0;
}

In above example, we just use istream and ostream references.
In this case, we can include the forward declaration of these
classes, So if you include just <iosfwd>, it's OK.

#include <iosfwd>
struct istream_wrapper
{
istream_wrapper(std::istream& is_) : is(is_) {}
std::istream& is;
};

struct ostream_wrapper
{
ostream_wrapper(std::eek:stream& os_) : os(os_) {}
std::eek:stream& os;
};
// same as before

There are some good points in Herb Sutter's Weblog. For instance
please see the following:
http://www.gotw.ca/gotw/007.htm

HTH
-- Saeed Amrollahi
 
G

Gil

Suppose I have a program which uses 'ostream' type as the parameter
type for a function and 'cout' object for writing into the standard
output. Following is my understanding regarding, which headers to
#include for this scenario.

Since the program uses 'cout' object, I have to #include <iostream>
header for 'cout'. In the ISO/IEC-14882:2003 document, in page 608, in
section 27.3 - Standard iostream objects, the synopsis for the header
<iostream> mentions the following line(apart from other statements):
     extern ostream cout;
From the above statement, since 'cout' is mentioned for <iostream>,
#including <iostream> is sufficient for using 'cout' object in my
program. Moreover, since 'ostream' is also mentioned in the same
statement, the forward declaraion of 'ostream' type should also be
known in the <iostream> header. This means that I need NOT #include
<iosfwd>. Is this
correct ? Since the class definition of the type 'ostream' is
needed(because the function parameter type is 'ostream&'), I HAVE to
#include <ostream> header instead of relying on the particular
compiler implementation #including the <ostream> inside <iostream>.
So, for the scenario mentioned in the beginning, I need ONLY to
#include
    #include <iostream>
    #include <ostream>
    // ...
    using namespace std;

I have NOT #included <iosfwd> header.

Is the above understanding of mine is correct ?
Correct me wherever I am wrong.

Kindly explain.

Thanks
V.Subramanian

by using a dubious logic, you reached the right conclusion.
I believe you need to read more about extern storage-class specifier
and forward declarations.

quick tips:
a) extern declaration of a non-nested type requires a declaration only
(not a definition) of that type e.g.

struct A; extern A a; //ok

b) references are similar to pointers in regard to forward
declarations.

#include <iosfwd>
std::eek:stream& print_out( std::eek:stream& ) const; //ok

c) in your own headers including <iosfwd> is _usually_ enough, even
inline operators can delegate to a print( stream& ) e.g.

#include <iosfwd>

struct A {
std::eek:stream& print_out( std::eek:stream& ) const; //ok
};

std::eek:stream& operator <<( std::eek:stream& os, const A & a ) {
return a.print_out( os ); //delegate to impl
}

but anyway check this out; refer to ISO/IEC 14882:2003(E):
22.2.8/5 example includes <iostream> but uses arithmetic extractor and
inserter;
however the same standard document doesn't guarantees in any way that
<iostream> header defines any extractor/inserter operators.
there are a couple of other examples exhibiting same assumption.

according to current C++ standard one must include both <iostream> and
<ostream> for something as simple as:

std::cout << "bye" << "\n";

note: I believe this is being worked with:
http://www.open-std.org/jtc1/sc22/wg21/docs/lwg-closed.html#343
http://www.open-std.org/jtc1/sc22/wg21/docs/lwg-defects.html#1178

hth,
gil
 
J

James Kanze

Suppose I have a program which uses 'ostream' type as the
parameter type for a function and 'cout' object for writing
into the standard output. Following is my understanding
regarding, which headers to #include for this scenario.
Since the program uses 'cout' object, I have to #include <iostream>
header for 'cout'. In the ISO/IEC-14882:2003 document, in page 608, in
section 27.3 - Standard iostream objects, the synopsis for the header
<iostream> mentions the following line(apart from other statements):
extern ostream cout;
From the above statement, since 'cout' is mentioned for <iostream>,
#including <iostream> is sufficient for using 'cout' object in my
program.

According to the current standard, no. You can declare an
extern with an incomplete type. Which means that the compiler
doesn't know about <<. With all known implementations, however,
yes, and C++0x wil require that <iostream> include <istream> and
Moreover, since 'ostream' is also mentioned in the same
statement, the forward declaraion of 'ostream' type should also be
known in the <iostream> header. This means that I need NOT #include
<iosfwd>. Is this correct ?

I think that the current standard would even allow some sort of
compiler magic, which means that you might not even be able to
use ostream as an incomplete type.
Since the class definition of the type 'ostream' is
needed(because the function parameter type is 'ostream&'),

If the type is a reference, it doesn't have to be complete.
I HAVE to #include <ostream> header instead of relying on the
particular compiler implementation #including the <ostream>
inside <iostream>.

Formally, according to the current standard, *if* you need a
So, for the scenario mentioned in the beginning, I need ONLY to
#include
#include <iostream>
#include <ostream>
// ...
using namespace std;
I have NOT #included <iosfwd> header.
Is the above understanding of mine is correct ?
Correct me wherever I am wrong.

If you include <iostream>, you don't have to include anything
else (or maybe <streambuf>, if you need it).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top