Google Groups unterstützt keine neuen Usenet-Beiträge oder ‑Abos mehr. Bisherige Inhalte sind weiterhin sichtbar.

Standard hello world problem

1 Aufruf
Direkt zur ersten ungelesenen Nachricht

Kenny Simpson

ungelesen,
02.01.2002, 23:55:4002.01.02
an
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...@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]

James Kuyper Jr.

ungelesen,
03.01.2002, 10:31:4103.01.02
an
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.

Dietmar Kuehl

ungelesen,
03.01.2002, 10:35:4303.01.02
an
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:dietma...@yahoo.com> <http://www.dietmar-kuehl.de/>
Phaidros eaSE - Easy Software Engineering: <http://www.phaidros.com/>

Hans Aberg

ungelesen,
03.01.2002, 10:36:4003.01.02
an
In article <b6c0f7ca.0201...@posting.google.com>,

ken...@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...@member.ams.org>
* Home Page: <http://www.matematik.su.se/~haberg/>
* AMS member listing: <http://www.ams.org/cml/>

Michiel Salters

ungelesen,
03.01.2002, 13:08:3903.01.02
an
In article <b6c0f7ca.0201...@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...@cmg.nl

Dietmar Kuehl

ungelesen,
04.01.2002, 10:33:0704.01.02
an
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:dietma...@yahoo.com> <http://www.dietmar-kuehl.de/>
Phaidros eaSE - Easy Software Engineering: <http://www.phaidros.com/>

---

0 neue Nachrichten