Respecting the standard

Discussion in 'C++' started by Grumble, Dec 17, 2003.

  1. Grumble

    Grumble Guest

    Hello,

    I was recently told that there were <quote>a lot of things
    wrong</quote> with the following program:

    #include <iostream>

    int main()
    {
    cout << '\a';
    return 0;
    }

    I believe I should have written std::cout instead of cout.
    Alternatively, I think I could have written:

    using namespace std; // I can now write 'cout' and 'endl'

    Second, I might need to write a newline to cout, otherwise the input
    might be discarded, is that correct?

    I'll make these two adjustments:

    #include <iostream>

    int main()
    {
    std::cout << '\a' << std::endl;
    return 0;
    }

    Do you see anything wrong with this program as far as standard C++
    is concerned? Did I really need to write endl to cout?

    g++ -ansi -pedantic -Wall -W foo.cxx does not give any warning, but
    I don't know how closely g++ adheres to the standard.
    Grumble, Dec 17, 2003
    #1
    1. Advertising

  2. Grumble <> writes:

    > Hello,
    >
    > I was recently told that there were <quote>a lot of things
    > wrong</quote> with the following program:
    >
    > #include <iostream>
    >
    > int main()
    > {
    > cout << '\a';
    > return 0;
    > }
    >
    > I believe I should have written std::cout instead of
    > cout. Alternatively, I think I could have written:
    >
    > using namespace std; // I can now write 'cout' and 'endl'


    Yep, both alternatives are correct.

    > Second, I might need to write a newline to cout, otherwise the input
    > might be discarded, is that correct?


    ITYM output instead of input - it doesn't get discarded, but the output
    buffer will not necessarily be flushed, so it will look like it got
    discarded.

    >
    > I'll make these two adjustments:
    >
    > #include <iostream>
    >
    > int main()
    > {
    > std::cout << '\a' << std::endl;
    > return 0;
    > }
    >
    > Do you see anything wrong with this program as far as standard C++ is
    > concerned?


    No, it's fine.

    > Did I really need to write endl to cout?
    >
    > g++ -ansi -pedantic -Wall -W foo.cxx does not give any warning, but I
    > don't know how closely g++ adheres to the standard.


    If it's g++ 3.x, it's quite close.

    HTH & kind regards
    frank

    --
    Frank Schmitt
    4SC AG phone: +49 89 700763-0
    e-mail: frankNO DOT SPAMschmitt AT 4sc DOT com
    Frank Schmitt, Dec 17, 2003
    #2
    1. Advertising

  3. Grumble

    tom_usenet Guest

    On Wed, 17 Dec 2003 10:44:09 +0100, Grumble <>
    wrote:

    >Hello,
    >
    >I was recently told that there were <quote>a lot of things
    >wrong</quote> with the following program:
    >
    >#include <iostream>
    >
    >int main()
    >{
    > cout << '\a';
    > return 0;
    >}
    >
    >I believe I should have written std::cout instead of cout.
    >Alternatively, I think I could have written:
    >
    >using namespace std; // I can now write 'cout' and 'endl'
    >
    >Second, I might need to write a newline to cout, otherwise the input
    >might be discarded, is that correct?
    >
    >I'll make these two adjustments:
    >
    >#include <iostream>
    >
    >int main()
    >{
    > std::cout << '\a' << std::endl;
    > return 0;
    >}


    Well, I'd adjust it to:

    #include <iostream>
    #include <ostream> //required for non-members

    int main()
    {
    std::cout << "\a\n";
    return 0;
    //cout implicitly flushed on program exit.
    }

    >Do you see anything wrong with this program as far as standard C++
    >is concerned?


    Yes - you need to include <ostream> to use endl (although most
    compilers don't care if <iostream> is included).

    > Did I really need to write endl to cout?


    I think portable programs should finish stdoutput with a newline.

    >g++ -ansi -pedantic -Wall -W foo.cxx does not give any warning, but
    >I don't know how closely g++ adheres to the standard.


    The latest version is pretty close.

    Tom

    C++ FAQ: http://www.parashift.com/c -faq-lite/
    C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
    tom_usenet, Dec 17, 2003
    #3
  4. Grumble

    Jeff Schwab Guest

    Grumble wrote:
    > Hello,
    >
    > I was recently told that there were <quote>a lot of things wrong</quote>
    > with the following program:
    >
    > #include <iostream>
    >
    > int main()
    > {
    > cout << '\a';
    > return 0;


    That return statement is redundant (in C++).

    As a purely academic nitpick that happened to come up here recently, you
    might need to include <ostream> if you want to use std::endl and keep
    the program portable.

    > }
    >
    > I believe I should have written std::cout instead of cout.
    > Alternatively, I think I could have written:
    >
    > using namespace std; // I can now write 'cout' and 'endl'
    >
    > Second, I might need to write a newline to cout, otherwise the input
    > might be discarded, is that correct?
    >
    > I'll make these two adjustments:
    >
    > #include <iostream>
    >
    > int main()
    > {
    > std::cout << '\a' << std::endl;
    > return 0;
    > }
    >
    > Do you see anything wrong with this program as far as standard C++ is
    > concerned? Did I really need to write endl to cout?
    >
    > g++ -ansi -pedantic -Wall -W foo.cxx does not give any warning, but I
    > don't know how closely g++ adheres to the standard.
    >
    Jeff Schwab, Dec 17, 2003
    #4
  5. In article <>,
    tom_usenet <> wrote:

    > Well, I'd adjust it to:
    >
    > #include <iostream>
    > #include <ostream> //required for non-members
    >
    > int main()
    > {
    > std::cout << "\a\n";
    > return 0;
    > //cout implicitly flushed on program exit.
    > }


    Just curious: Have you come across any implementation where the
    <ostream> is actually required? There is considerable debate on whether
    we really want to break every single C++ text which has shown the
    traditional "HelloWorld" with only <iostream>. ;-)

    Martin Sebor has done an admirable job of trying to bring this issue to
    the committee. Unfortunately I don't think he has entirely succeeded
    yet.

    -Howard
    Howard Hinnant, Dec 17, 2003
    #5
  6. Grumble

    tom_usenet Guest

    On Wed, 17 Dec 2003 14:29:26 GMT, Howard Hinnant
    <> wrote:

    >In article <>,
    > tom_usenet <> wrote:
    >
    >> Well, I'd adjust it to:
    >>
    >> #include <iostream>
    >> #include <ostream> //required for non-members
    >>
    >> int main()
    >> {
    >> std::cout << "\a\n";
    >> return 0;
    >> //cout implicitly flushed on program exit.
    >> }

    >
    >Just curious: Have you come across any implementation where the
    ><ostream> is actually required?


    Nope, other than the Deathstation 9000. I thought that Dietmar's cxxrt
    might be one, but it too includes <istream> and <ostream> in
    <iostream>.

    There is considerable debate on whether
    >we really want to break every single C++ text which has shown the
    >traditional "HelloWorld" with only <iostream>. ;-)


    Many hello world programs seem to use std::endl for no good reason,
    and I could certainly envision an implementation that doesn't expose
    endl unless ostream is explicitly included. But not to include the
    non-member operator<<'s (for char*) would be a bit crazed - hello
    world output would become a random pointer value!

    >Martin Sebor has done an admirable job of trying to bring this issue to
    >the committee. Unfortunately I don't think he has entirely succeeded
    >yet.


    Good luck to him! It doesn't seem very important though, just ironic
    that the canonical hello world program relies on unspecified
    behaviour.

    Tom

    C++ FAQ: http://www.parashift.com/c -faq-lite/
    C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
    tom_usenet, Dec 17, 2003
    #6
  7. In article <>,
    tom_usenet <> wrote:

    > Many hello world programs seem to use std::endl for no good reason


    <nod><flush>
    I<flush>
    agree<flush>
    100%!<flush>

    > just ironic
    > that the canonical hello world program relies on unspecified
    > behaviour.


    <chuckle> :)

    -Howard
    Howard Hinnant, Dec 17, 2003
    #7
  8. On Wed, 17 Dec 2003 12:06:03 +0000, tom_usenet
    <> wrote:

    [snip]
    >>Do you see anything wrong with this program as far as standard C++
    >>is concerned?

    >
    >Yes - you need to include <ostream> to use endl (although most
    >compilers don't care if <iostream> is included).


    The header synopsis for <iostream> (section 27.3) declares std::cout
    as "extern ostream cout;" in namespace std. std::cin is declared as
    "extern istream cin;" Therefore, it must include both <istream> and
    <ostream>, as far as I can tell.

    >> Did I really need to write endl to cout?

    >
    >I think portable programs should finish stdoutput with a newline.
    >


    std::cin and std::cout should do whatever stdin and stdout are
    supposed to do.

    >>g++ -ansi -pedantic -Wall -W foo.cxx does not give any warning, but
    >>I don't know how closely g++ adheres to the standard.

    >
    >The latest version is pretty close.
    >
    >Tom
    >
    >C++ FAQ: http://www.parashift.com/c -faq-lite/
    >C FAQ: http://www.eskimo.com/~scs/C-faq/top.html


    --
    Bob Hairgrove
    Bob Hairgrove, Dec 17, 2003
    #8
  9. Grumble

    tom_usenet Guest

    On Wed, 17 Dec 2003 20:20:48 GMT, wouldnt_you_like@to_know.com (Bob
    Hairgrove) wrote:

    >On Wed, 17 Dec 2003 12:06:03 +0000, tom_usenet
    ><> wrote:
    >
    >[snip]
    >>>Do you see anything wrong with this program as far as standard C++
    >>>is concerned?

    >>
    >>Yes - you need to include <ostream> to use endl (although most
    >>compilers don't care if <iostream> is included).

    >
    >The header synopsis for <iostream> (section 27.3) declares std::cout
    >as "extern ostream cout;" in namespace std. std::cin is declared as
    >"extern istream cin;" Therefore, it must include both <istream> and
    ><ostream>, as far as I can tell.


    Nope, it just needs complete definitions of basic_istream and
    basic_ostream (and the typedefs istream and ostream). Imagine if
    <istream> is just

    #include <iosfwd>
    #include <impl/istream_core.h>
    #include <impl/istream_non_members.h>


    and <iostream> is

    #include <iosfwd>
    #include <impl/istream_core.h>
    extern istream cin;
    extern wistream wcin;
    //...

    Now you don't get std::endl (or operator<<(ostream, char const*)!).

    >
    >>> Did I really need to write endl to cout?

    >>
    >>I think portable programs should finish stdoutput with a newline.
    >>

    >
    >std::cin and std::cout should do whatever stdin and stdout are
    >supposed to do.


    Right, it is implementation defined (according to the C standard)
    whether text streams (such as stdout) require a terminating newline
    character.

    Tom

    C++ FAQ: http://www.parashift.com/c -faq-lite/
    C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
    tom_usenet, Dec 18, 2003
    #9
    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. johndesp
    Replies:
    0
    Views:
    620
    johndesp
    May 7, 2004
  2. Shawn Modersohn

    nestscape respecting floats

    Shawn Modersohn, Apr 26, 2004, in forum: HTML
    Replies:
    2
    Views:
    430
    Shawn Modersohn
    Apr 27, 2004
  3. Jim
    Replies:
    8
    Views:
    386
    Raymond Hettinger
    Jul 10, 2006
  4. Steve Hershoff
    Replies:
    0
    Views:
    394
    Steve Hershoff
    Aug 9, 2006
  5. R. David Murray
    Replies:
    8
    Views:
    582
    Tim Chase
    Mar 27, 2009
Loading...

Share This Page