Requirements for <iostream>

Discussion in 'C++' started by Old Wolf, Mar 5, 2007.

  1. Old Wolf

    Old Wolf Guest

    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) ?
     
    Old Wolf, Mar 5, 2007
    #1
    1. Advertising

  2. Old Wolf

    Bo Persson Guest

    Old Wolf wrote:
    > 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).


    We have the <iosfwd> header, that is intended to use where you need
    declarations but not definitions.

    >
    > 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
     
    Bo Persson, Mar 6, 2007
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Lans Redmond

    iostream::read function

    Lans Redmond, Jun 26, 2003, in forum: C++
    Replies:
    0
    Views:
    1,172
    Lans Redmond
    Jun 26, 2003
  2. John Tiger
    Replies:
    10
    Views:
    5,666
  3. ai@work
    Replies:
    9
    Views:
    569
    Ron Natalie
    Dec 16, 2004
  4. S. Nurbe

    iostream + iostream.h

    S. Nurbe, Jan 14, 2005, in forum: C++
    Replies:
    7
    Views:
    805
    red floyd
    Jan 15, 2005
  5. red floyd
    Replies:
    3
    Views:
    557
    Dietmar Kuehl
    Mar 8, 2005
Loading...

Share This Page