Requirements for <iostream>

O

Old Wolf

In a discussion elsewhere, someone wrote:

#include <iostream> is not guaranteed to define std::cout, it
merely has to declare it as an extern object, and declarations
of extern objects do not require complete types. As far as I
understand, a implementation is free to use a certain amount
of compiler magic to be able to declare std::cout etc. without
defining basic_ostream.

Is this correct? If so, then it would mean that the following program
is invalid:

#include <iostream>
int main() { std::cout; }

I don't see how an extern definition can be provided but the type
be incomplete (unless it is a definition of an array of unknown size).

Secondly, section 27.3 of the standard (1998) says that <iostream>
must define:
extern ostream cout;

but says nothing else about ostream. Does this imply that std::eek:stream
must be declared at this point (ie. basic_ostream<> must have been
declared, and typedef'd to ostream as well) ?
 
B

Bo Persson

Old said:
In a discussion elsewhere, someone wrote:

#include <iostream> is not guaranteed to define std::cout, it
merely has to declare it as an extern object, and declarations
of extern objects do not require complete types. As far as I
understand, a implementation is free to use a certain amount
of compiler magic to be able to declare std::cout etc. without
defining basic_ostream.

Is this correct? If so, then it would mean that the following program
is invalid:

#include <iostream>
int main() { std::cout; }

Probably. What does it do?
I don't see how an extern definition can be provided but the type
be incomplete (unless it is a definition of an array of unknown size).


Secondly, section 27.3 of the standard (1998) says that <iostream>
must define:
extern ostream cout;

Definitely

#include <iosfwd>

namespace std
{
extern ostream cout;
}

must be enough. Note that <iosfwd> contains the typedef for ostream.

If you want to use any of the operators on a stream, you have to include
<ostream> or <istream> as well. That's were operators << and >> are defined
(most of them, anyway).


Bo Persson
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top