Topic: Standard hello world problem
Author: remove.haberg@matematik.su.se (Hans Aberg)
Date: Thu, 3 Jan 2002 15:36:40 GMT Raw View
In article <b6c0f7ca.0201012323.6437cca@posting.google.com>,
kennys@voicenet.com (Kenny Simpson) wrote:
>According to popular belief (at least that I've seen),
>the following if perfectly legal C++, and is even used
>in many places as the canonical example:
>
>#include <iostream>
>int main()
>{
> std::cout << "hello world" << std::endl;
>}
>
>However, from reading the Standard and the L&K IOStreams
>book, <iostream> is only guaranteed to declare std::cin,
>std::cout, std::cerr, std::clog and the wide versions of
>these.
...
>Leaving std::ostream to be an incomplete type and causing
>the first operator<< to fail to compile.
...
>So, a conforming <iostream> may look like:
>#include <iosfwd> // for forward declarations ...
...
>#include <iostream> // for std::cout
>#include <ostream> // for std::ostream and std::endl
>int main()
>{
> std::cout << "hello world" << std::endl;
>}
>Please tell me I missed something (and tell me what it is)
It looks as though what you say, namely that according the way the
standard is written, one does not get any sizes of the standard stream
objects, so they cannot be used without those headers separately being
included.
So either the Standard C++ Gurus have changed or it is a mistake: A defect
report.
I tend to believe that this is a defect report, so that the "Hello World"
program only needs to include <iostream>.
Hans Aberg * Anti-spam: remove "remove." from email address.
* Email: Hans Aberg <remove.haberg@member.ams.org>
* Home Page: <http://www.matematik.su.se/~haberg/>
* AMS member listing: <http://www.ams.org/cml/>
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
Author: Michiel Salters<Michiel.Salters@cmg.nl>
Date: Thu, 3 Jan 2002 18:08:39 GMT Raw View
In article <b6c0f7ca.0201012323.6437cca@posting.google.com>, Kenny Simpson
says...
>
>Hey,
>According to popular belief (at least that I've seen),
>the following if perfectly legal C++, and is even used
>in many places as the canonical example:
>
>#include <iostream>
>int main()
>{
> std::cout << "hello world" << std::endl;
>}
>
>However, from reading the Standard and the L&K IOStreams
>book, <iostream> is only guaranteed to declare std::cin,
>std::cout, std::cerr, std::clog and the wide versions of
>these. It also may include some magic to ensure that
>these global objects are constructed before any other
>object, and are destroyed after all other objects.
Quoting from the standard:"
2 Mixing operations on corresponding wide- and narrow-character streams
follows the same semantics as mixing such operations on FILEs, as specified
in Amendment 1 of the ISO C standard. The objects are constructed, and the
associations are established at some time prior to or during first time an
object of class basic_ios<charT,traits>::Init is constructed, and in any
case before the body of main begins execution.264)"
So <iostream> may include some magic, but if it doesn't, something else
must contain said magic.
>So, a conforming <iostream> may look like:
>#include <iosfwd>
>namespace std {
> extern ostream cout;
> ...
>}
>Leaving std::ostream to be an incomplete type and causing
>the first operator<< to fail to compile.
Except that somewhere else a definition of cout must be done,
so std::ostream must be complete (there). (See e.g. the note in 3.2/4)
You have a valid point, though - that definition can be in another
translation unit, so in main() the type std::ostream might not be
complete in main().
>As if that weren't enought, std::endl is declared in
><ostream>, and is not mentioned at all in <iostream>.
That's true. Writing "Hello,world\n" is a quick fix, but
clearly not how things should be.
>>From my reading it seems that the Standard-conforming way
>to write good old 'hello world' is:
>
>#include <iostream> // for std::cout
>#include <ostream> // for std::ostream and std::endl
>
>int main()
>{
> std::cout << "hello world" << std::endl;
>}
>
>Please tell me I missed something (and tell me what it is)
>
>Thanks,
>-Kenny
The problem I see is that the non-normative examples in the standard
use std::endl as if if was defined in <iostream>, so the intention
clearly is that <iostream> includes (part of) <ostream>, but I can't
find wording either that says so.
Perhaps someone else can show why <iostream> must include <ostream> ?
Regards,
--
Michiel Salters
Consultant Technical Software Engineering
CMG Trade, Transport & Industry
Michiel.Salters@cmg.nl
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
Author: Dietmar Kuehl <dietmar_kuehl@yahoo.com>
Date: Fri, 4 Jan 2002 15:33:07 GMT Raw View
Hi,
Michiel Salters wrote:
> Perhaps someone else can show why <iostream> must include <ostream> ?
You can't show it on the ground of the standard because the standard
does not say that <iostream> must include <ostream> or provide
definitions of 'std::basic_ostream', etc. The standard mandates that
<iostream> declares the eight standard stream objects ('std::cout',
'std::cin', 'std::cerr', and 'std::clog' plus their wide character
counterparts). To do so, <iostream> must declare a bunch of class
templates ('std::char_traits', 'std::basic_ostream', and
'std::basic_istream') and that's it.
I mentioned this problem several years ago in this forum (just for
reference here is the link:
<http://groups.google.com/groups?selm=6p6vnr%245v7%241%40nnrp1.dejanews.com&output=gplain>;
I had trouble finding it myself...). It was considered a defect but
I'm not sure whether there is indeed a defect on the DR list on this
one..
--
<mailto:dietmar_kuehl@yahoo.com> <http://www.dietmar-kuehl.de/>
Phaidros eaSE - Easy Software Engineering: <http://www.phaidros.com/>
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
Author: kennys@voicenet.com (Kenny Simpson)
Date: Thu, 3 Jan 2002 04:55:40 GMT Raw View
Hey,
According to popular belief (at least that I've seen),
the following if perfectly legal C++, and is even used
in many places as the canonical example:
#include <iostream>
int main()
{
std::cout << "hello world" << std::endl;
}
However, from reading the Standard and the L&K IOStreams
book, <iostream> is only guaranteed to declare std::cin,
std::cout, std::cerr, std::clog and the wide versions of
these. It also may include some magic to ensure that
these global objects are constructed before any other
object, and are destroyed after all other objects.
So, a conforming <iostream> may look like:
#include <iosfwd> // for forward declarations of
// std::basic_ostream and
// std::ostream typedef
namespace std
{
extern ostream cout;
...
}
Leaving std::ostream to be an incomplete type and causing
the first operator<< to fail to compile.
As if that weren't enought, std::endl is declared in
<ostream>, and is not mentioned at all in <iostream>.
>From my reading it seems that the Standard-conforming way
to write good old 'hello world' is:
#include <iostream> // for std::cout
#include <ostream> // for std::ostream and std::endl
int main()
{
std::cout << "hello world" << std::endl;
}
Please tell me I missed something (and tell me what it is)
Thanks,
-Kenny
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
Author: "James Kuyper Jr." <kuyper@wizard.net>
Date: Thu, 3 Jan 2002 15:31:41 GMT Raw View
Kenny Simpson wrote:
>
> Hey,
> According to popular belief (at least that I've seen),
> the following if perfectly legal C++, and is even used
> in many places as the canonical example:
>
> #include <iostream>
> int main()
> {
> std::cout << "hello world" << std::endl;
> }
...
> >From my reading it seems that the Standard-conforming way
> to write good old 'hello world' is:
>
> #include <iostream> // for std::cout
> #include <ostream> // for std::ostream and std::endl
>
> int main()
> {
> std::cout << "hello world" << std::endl;
> }
>
> Please tell me I missed something (and tell me what it is)
I believe that you're correct; portable code must be written that way.
However, any C++ standard header may #include any other standard header,
so I wouldn't be surprised if there are a lot of implementations where
#include <ostream> is unnecessary.
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
Author: Dietmar Kuehl <dietmar_kuehl@yahoo.com>
Date: Thu, 3 Jan 2002 15:35:43 GMT Raw View
Hi,
Kenny Simpson wrote:
>From my reading it seems that the Standard-conforming way
> to write good old 'hello world' is:
>
> #include <iostream> // for std::cout
> #include <ostream> // for std::ostream and std::endl
>
> int main()
> {
> std::cout << "hello world" << std::endl;
> }
>
> Please tell me I missed something (and tell me what it is)
This is definitely what the standard says and I raised this very point
ages ago (could be four to five years ago). The general response was
that all implementation allow ommission of <ostream> (except mine at
this point which I have "fixed" afterwards) and that the intentation
was that <iostream> implicitly include <istream> and <ostream>. I don't
know whether this stuff made it into the DR list and is possibly fixed.
--
<mailto:dietmar_kuehl@yahoo.com> <http://www.dietmar-kuehl.de/>
Phaidros eaSE - Easy Software Engineering: <http://www.phaidros.com/>
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]