Thread: Can you test this for me?

  1. #1
    Slave MadCow257's Avatar
    Join Date
    Jan 2005
    Posts
    735

    Can you test this for me?

    Code:
    #include <iostream> // Required for std::cout
    
    int main()
    {
        std::cout << "Hello World!" << std::endl;
        return 0;
    }
    I woud like to know for all of the common modern compilers wether it would succesfully build. (There aren't that many)

    Why? I want to prove a point over at Wikipedia that ostream does not need to be explicitly included.

    I have already confirmed that it works with all of microsoft's compilers.

    Thanks,
    MadCow

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    http://groups.google.com/group/comp....0ad170b176f50?
    Quote Originally Posted by Mike Wahler
    Note: while most implementations will work without it, the declaration for 'endl' is provided by <ostream>. It is allowed to be, but not required to be, provided by <iostream>. 'endl' is also in namespace 'std'. (Its 'full name' is 'std::endl')
    ?
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    Aug 2003
    Posts
    1,218
    It will work because it is perfectly standard C++. According to C++ standard 27.3 iostream must contain the following:
    Code:
    namespace std
    {
      extern istream cin;
      extern ostream cout;
      // some other stuff related to streams.
    }
    Edit: Doesnt that contradict what he says because, since the standard says that an extern ostream cout has to be in iostream, then doesnt ostream have to be included in order to be able to declare cout as an object of type ostream??
    Last edited by Shakti; 03-03-2006 at 03:08 PM.
    STL Util a small headers-only library with various utility functions. Mainly for fun but feedback is welcome.

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Pay attention to this part: endl.

    An apparently deleted reply:
    According to both the standard and barne iostream includes ostream. So if it doesn't work its a compiler issue
    http://public.research.att.com/~bs/3rd_issues.html
    Quote Originally Posted by Stroustrup
    pg 633: I say "Manipulators taking istream and ostream are presented in and < ostream> respectively, and also in < iostream>. The rest of the standard manipulators are presented in < iomanip>." However, the standard disagrees and provides no manipulators in < iostream>, thus making this usage non-conforming:
    Code:
    #include < iostream>
    int main()
    {
        std::cout << "Hello world" << std::endl;
        return 0;
    }
    Personally, I prefer the simpler (and valid)
    Code:
    #include < iostream>
    int main()
    {
        std::cout << "Hello world\n";
    }
    but I expect that there are enough programs depending on endl for the committee to seriously consider changing the standard to match the implementations.
    [Colors (and code tags) added by me.]
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  5. #5
    Registered User
    Join Date
    Aug 2003
    Posts
    1,218
    And I stand corrected
    STL Util a small headers-only library with various utility functions. Mainly for fun but feedback is welcome.

  6. #6
    Slave MadCow257's Avatar
    Join Date
    Jan 2005
    Posts
    735
    Hmm and so do I. My edit's probably gonna stand over at the wiki though, so everything's good.

  7. #7
    Registered User Queatrix's Avatar
    Join Date
    Apr 2005
    Posts
    1,342
    I allways get errors using "std::SOMTHING() but not when I declare "useing namespace std;" then call "SOMTHING()". I have to change all of those when I get example script.

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> My edit's probably gonna stand over at the wiki though, so everything's good.

    You mean you edited it to include incorrect information and it will stand, or your edit allows for the fact that the original code is non-standard?

    Which page?

  9. #9
    Slave MadCow257's Avatar
    Join Date
    Jan 2005
    Posts
    735
    You mean you edited it to include incorrect information and it will stand
    Indeed I did, but I'm beggining to lean towards just reverting it to spare the conflict and look at editing other things.

    My logic is this:
    Imagine two compilers, both 100% standards compliant. One needs ostream explicitly, the other does not. Which is better? The latter, because that is what is popular.

    This is the program in question
    http://en.wikipedia.org/wiki/C%2B%2B..._world_program

  10. #10
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by MadCow257
    My logic is this:
    Imagine two compilers, both 100% standards compliant. One needs ostream explicitly, the other does not. Which is better? The latter, because that is what is popular.
    This is the same argument as being in favor of fflush(stdin) or void main(), which are generally not met favorably here. Standard code is always preferred, becuase then theoretically we don't have to keep writing the same three lines of code over and over and over and over for each implementation that pops up over the years.

    >because that is what is popular

    Reminds me of the old joke:
    Q. How many Microsoft programmers does it take to change a light bulb?
    A. None -- they'll just change the standard to darkness.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  11. #11
    Slave MadCow257's Avatar
    Join Date
    Jan 2005
    Posts
    735
    void main is a bad example because the stanard requires main to return an int

    My argument is completely different both versions are 100% compliant with the standard. I would like to think of my change as the nature of "business." As I said though, I'm not going to continue this argument over at wikipedia.

  12. #12
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Quote Originally Posted by MadCow257
    void main is a bad example because the stanard requires main to return an int
    If I understood correctly, I think his example was meant to explain that even though some compilers might accept non-standard code, such as std::endl without explicitly including <ostream> or using void main() (which some compilers will accept) it's still better to meet the standard expectations that you do declare main as an integer and you do either explicitly declare <ostream> when you use endl or you just don't use it at all.

    I could be wrong though.
    Sent from my iPadŽ

  13. #13
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> One needs ostream explicitly, the other does not.

    IMO, the former is clearly better. Code written on that compiler will work on all standards compliant compilers, while code written for the latter compiler may unknowingly take advantage of the non-standard "extension" and fail to compile in another environment.

    An example of this is <string>. Parts of <string> are included in <iostream> in some modern compiler libraries. This has led to many newbies using string without #including <string>. In the case of at least one popular compiler, omitting <string> can lead to errors when the user attempts to output the string using <<. Questions have been asked on this forum and others about why outputting a string doesn't work. There are people who are convinced that you must call c_str() to output a string using cout. All of this could have been avoided if the library writers had followed a more strict interpretation of the standard.

    While I don't consider it to be a big deal, IMO all Hello World examples should be using '\n' instead of endl (or #including <ostream>) just to maintain complete correctness.
    Last edited by Daved; 03-03-2006 at 04:55 PM.

  14. #14
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    I would say Daved's example stands as the best, so far.
    Sent from my iPadŽ

  15. #15
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    some excellent points, just a quickie

    Daved, if what you say is true then why was I taught at college in C++ to use endl and only use "\n" when the need was justified?

    I am not saying what you think is wrong, but I am confused why a college proffesor in computer science would think of using endl over "\n" and others would not?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Integer Emulation
    By Elysia in forum C++ Programming
    Replies: 31
    Last Post: 03-18-2008, 01:03 PM
  2. undefined reference
    By 3saul in forum Linux Programming
    Replies: 12
    Last Post: 08-23-2006, 05:28 PM
  3. C++ Operator Overloading help
    By Bartosz in forum C++ Programming
    Replies: 2
    Last Post: 08-17-2005, 12:55 PM
  4. MSVC Template Constructor/Assignment Errors
    By LuckY in forum Windows Programming
    Replies: 3
    Last Post: 07-22-2005, 02:57 PM
  5. Question About Linker Errors In Dev-C++
    By ShadowMetis in forum C++ Programming
    Replies: 9
    Last Post: 08-18-2004, 08:42 PM