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

Discussion in 'C++' started by subramanian100in@yahoo.com, India, Nov 13, 2010.

  1. , India

    , India Guest

    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
     
    , India, Nov 13, 2010
    #1
    1. Advertising

  2. On Nov 13, 7:05 am, ", India"
    <> 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. 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
     
    Saeed Amrollahi, Nov 13, 2010
    #2
    1. Advertising

  3. , India

    Gil Guest

    On Nov 12, 11:05 pm, ", India"
    <> 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. 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
     
    Gil, Nov 13, 2010
    #3
  4. , India

    James Kanze Guest

    On Nov 13, 4:05 am, ", India"
    <> 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
     
    James Kanze, Nov 13, 2010
    #4
    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. John Tiger
    Replies:
    10
    Views:
    5,666
  2. ai@work
    Replies:
    9
    Views:
    569
    Ron Natalie
    Dec 16, 2004
  3. S. Nurbe

    iostream + iostream.h

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

    <iosfwd> & typedefs with incomplete types.

    Steven T. Hatton, Jul 7, 2005, in forum: C++
    Replies:
    0
    Views:
    374
    Steven T. Hatton
    Jul 7, 2005
Loading...

Share This Page