f



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

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

0
subramanian100in
11/13/2010 4:05:38 AM
comp.lang.c++ 49423 articles. 7 followers. Post Follow

3 Replies
1009 Views

Similar Articles

[PageSpeed] 27

On Nov 13, 7:05=A0am, "subramanian10...@yahoo.com, India"
<subramanian10...@yahoo.com> wrote:
> 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):
> =A0 =A0 =A0extern 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
> =A0 =A0 #include <iostream>
> =A0 =A0 #include <ostream>
> =A0 =A0 // ...
> =A0 =A0 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::ostream& os_) : os(os_) {}
  std::ostream& 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::ostream& os_) : os(os_) {}
  std::ostream& 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
0
Saeed
11/13/2010 7:31:07 AM
On Nov 12, 11:05=A0pm, "subramanian10...@yahoo.com, India"
<subramanian10...@yahoo.com> wrote:
> 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):
> =A0 =A0 =A0extern 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
> =A0 =A0 #include <iostream>
> =A0 =A0 #include <ostream>
> =A0 =A0 // ...
> =A0 =A0 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::ostream& print_out( std::ostream& ) 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::ostream& print_out( std::ostream& ) const; //ok
};

std::ostream& operator <<( std::ostream& 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
0
Gil
11/13/2010 8:21:20 AM
On Nov 13, 4:05 am, "subramanian10...@yahoo.com, India"
<subramanian10...@yahoo.com> wrote:
> 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
<ostream>.

> 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.
<iosfwd> is sufficient (and all I use in my headers).

> 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
complete type, you have to include <ostream>.  Practically, you
don't, and the next version of the standard will formalize this.

> 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).

--
James Kanze
0
James
11/13/2010 11:26:55 PM
Reply: