Topic: Hello, world!" does not [necessarily] compile!


Author: Dietmar_Kuehl@my-dejanews.com
Date: 1998/07/23
Raw View
Hi,

many textbooks about C++ feature the usual "Hello, world!"-program.
For example, C++PL3 shows this version in Section 3.2:

  #include <iostream>

  int main()
  {
    std::cout << "Hello, world!\n";
  }

Looks reasonable at first sight (however, see (*) below for a minor
catch). Unless I don't understand the implications of the library
clauses, this does not necessarily compile with all conforming
implementations! Here is why: The contents of the header <iostream> is
defined in Clause 27.3 (Standard stream objects).  This clause states
that <iostream> includes declarations of the eight standard stream
objects. Clause 7.1 (Specifiers) allows in Paragraph 8 that an extern
declaration may use an incomplete type. Thus, I think the following is
a legal implementation of the header '<iostream>':

--- BEGIN <iostream> ---
  #if !defined(__IOSTREAM__)
  #define __IOSTREAM__ 1

  namespace std
  {
    template <class charT> class char_traits;

  #if !defined(__BASIC_ISTREAM_DEF_ARG__)
  #define __BASIC_ISTREAM_DEF_ARG__ 1
    template <class charT, class traits = char_traits<charT> >
  #else
    template <class charT, class traits>
  #endif
    class basic_istream;

  #if !defined(__BASIC_OSTREAM_DEF_ARG__)
  #define __BASIC_OSTREAM_DEF_ARG__ 1
    template <class charT, class traits = char_traits<charT> >
  #else
    template <class charT, class traits>
  #endif
    class basic_ostream;

    extern basic_istream<char, char_traits<char> > cin;
    extern basic_ostream<char, char_traits<char> > cout;
    extern basic_ostream<char, char_traits<char> > cerr;
    extern basic_ostream<char, char_traits<char> > clog;

    extern basic_istream<wchar_t, char_traits<wchar_t> > wcin;
    extern basic_ostream<wchar_t, char_traits<wchar_t> > wcout;
    extern basic_ostream<wchar_t, char_traits<wchar_t> > wcerr;
    extern basic_ostream<wchar_t, char_traits<wchar_t> > wclog;
  }

  #endif
--- END <iostream> ---

All those preprocessor stuff is necessary to make sure that
- the header can be included multiple times
- headers can be included in any order; at the same time, default
  arguments can only be present in the first declaration of an
  entity

This compiles fine with the compilers I have and also accomplishes the
task of declaring the standard stream objects. But it does nothing
more. To use these objects, it would be necessary to also include one
of the headers *defining* the classes 'basic_istream' and 'basic_ostream',
ie. <istream> and <ostream>.

Is this analysis correct? If it is not, why not? Since I'm always
dreaming of an implementation which only supports stuff really required
by the standard, I can imagine that the above header could be used by
an implementation, if it is legal (at least, this header should be
used in "pedantic mode"). I think this implementation would surprise
a lot of users and it is probably not what is intended.
--
(*) Well, actually this function looks rather weird: It is a function
    returning something different than 'void' without a return
    statement. Normally, if control flows off such functions without a
    return statement, this results in "undefined behavior". The function
    'main()' is an explicit exception to this rule: If the control flows
    off this function without a return statement, it behaves as if the
    last statement were 'return 0;' (3.6.1 Main function, Paragraph
    5). Personally, I would prefer if 'main()' in the example would end
    with a return statement...
--
<mailto:dietmar.kuehl@claas-solutions.de>
homepage: <http://www.informatik.uni-konstanz.de/~kuehl>

-----== Posted via Deja News, The Leader in Internet Discussion ==-----
http://www.dejanews.com/rg_mkgrp.xp   Create Your Own Free Member Forum


[ 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://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: ncm@nospam.cantrip.org (Nathan Myers)
Date: 1998/07/24
Raw View
<Dietmar_Kuehl@my-dejanews.com> wrote:
>Many textbooks about C++ feature the usual "Hello, world!"-program.
>For example, C++PL3 shows this version in Section 3.2:
>
>  #include <iostream>
>  int main() { std::cout << "Hello, world!\n"; }
>
>Looks reasonable at first sight ...  [but] this does not necessarily
>compile with all conforming implementations!
>Here is why: The contents of the header <iostream> is
>defined in Clause 27.3 (Standard stream objects).  This clause
>[only] states that <iostream> includes declarations of the eight
>standard stream objects. ...
>To use these objects, it would be necessary to also include one
>a legal implementation of the header '<iostream>':
>ie. <istream> and <ostream>.
>
>Is this analysis correct?

I believe Dietmar is correct, and has identified a bug in the standard.
It should say that <iostream> includes <istream> and <ostream>.  There
will certainly be a defect report about this.

In the meantime, it is probably safe to continue running
"hello".

--
Nathan Myers
ncm@nospam.cantrip.org  http://www.cantrip.org/



[ 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://reality.sgi.com/austern_mti/std-c++/faq.html              ]