6.1 iosfwd and cout, cin, cerr

The standard header file does not forward reference the classes
cout, cerr and cin. This makes it necessary to include in a
header file that references one.

Is this strict adherence to The Standard, unhelpful interpretation of the
standard, or an ommission? :slight_smile:

Ken Appleby
Soft Optics Ltd

Ken Appleby <ken@soft-optics.co.uk> wrote:

The standard header file does not forward reference the classes
cout, cerr and cin. This makes it necessary to include in a
header file that references one.

Is this strict adherence to The Standard, unhelpful interpretation of the
standard, or an ommission? > :slight_smile:

This is correct, although I don’t suppose there is anything in the standard
that forbids from including (minimal header dependencies
can be inferred to an extent, but the implementor could include every header in
every other one if he felt like it I believe).

is the header declaring the standard stream objects (cin, cout,
etc), and therefore if you want to use them, it’s what you should be including!
As such, it actually typically adds execution overhead in both time and size
(both minor though) due to putting down a “static” initialisation object that
makes sure that cin, etc are constructed before you can possibly use them. So
include if and only if you specifically want cin, cout, etc.
will also have to include (which includes !). If
you want basic_ostream (or ostream), use . Bizarrely
contains both basic_istream and basic_iostream (hence the confusion with the
file , perhaps). Files require of course, which will be
including .

Finally, forward declares all of the various io classes (you can’t
forward declare an object of course) and adds the typedefs for istream,
ostream, etc. It doesn’t declare any objects though. Typically use in
headers where you have serialisation functions taking istream or ostream
reference parameters, to potentially reduce compile times (which is 's
raison d’etre).

Tom

In article <9najts$e5l$1@inn.qnx.com>,
Ken Appleby <ken@soft-optics.co.uk> wrote:

The standard header file does not forward reference the classes
cout, cerr and cin. This makes it necessary to include in a
header file that references one.

That’s right.

Is this strict adherence to The Standard, unhelpful interpretation of the
standard, or an ommission? > :slight_smile:

That’s the intent of the standard. is not a substitute
for . Once upon a time programs used to do something like so:

class ostream;
ostream& operator<<(ostream&, myclass&);

and not have to #include <iostream.h> (the pre-standard name).
But one implication of is that iostreams is now
templatized, therefore, “class ostream” is meaningless,
because the underlying class is not that name any longer.

Therefore, one either needs to drag in the while iostream,
or just enough to be able to get ostream established
(typedef’d to a certain character type’d template).

Of course, if you actually want to define the above function
instead of just prototyping it, you need the whole ,
and wanting to make use of something like cout also requires ti.

Greg Comeau export ETA: Dec 1 10% “End of Summer” Offer
Comeau C/C++ ONLINE ==> http://www.comeaucomputing.com/tryitout
World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
Comeau C/C++ with Dinkumware’s Libraries… Have you tried it?

In article <9namjs$flq$1@inn.qnx.com>, <tom_usenet@hotmail.com> wrote:

Ken Appleby <> ken@soft-optics.co.uk> > wrote:
The standard header file does not forward reference the classes
cout, cerr and cin. This makes it necessary to include in a
header file that references one.

Is this strict adherence to The Standard, unhelpful interpretation of the
standard, or an ommission? > :slight_smile:

This is correct, although I don’t suppose there is anything in the standard
that forbids from including (minimal header dependencies
can be inferred to an extent, but the implementor could include every header in
every other one if he felt like it I believe).

That’s correct for C++ (but not for C).

iostream> is the header declaring the standard stream objects (cin, cout,
etc), and therefore if you want to use them, it’s what you should be including!

Agreed.

As such, it actually typically adds execution overhead in both time and size
(both minor though) due to putting down a “static” initialisation object that
makes sure that cin, etc are constructed before you can possibly use them. So
include if and only if you specifically want cin, cout, etc.

Agreed (for some apps this doesn’t matter though).

iostream> will also have to include (which includes !). If
you want basic_ostream (or ostream), use . Bizarrely <istream
contains both basic_istream and basic_iostream (hence the confusion with the
file , perhaps). Files require of course, which will be
including .

Finally, forward declares all of the various io classes (you can’t
forward declare an object of course) and adds the typedefs for istream,
ostream, etc. It doesn’t declare any objects though. Typically use in
headers where you have serialisation functions taking istream or ostream
reference parameters, to potentially reduce compile times (which is 's
raison d’etre).

Yup.

Greg Comeau export ETA: Dec 1 10% “End of Summer” Offer
Comeau C/C++ ONLINE ==> http://www.comeaucomputing.com/tryitout
World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
Comeau C/C++ with Dinkumware’s Libraries… Have you tried it?