Topic: A newbie's question on standard headers


Author: hinnant@metrowerks.com (Howard Hinnant)
Date: 1998/03/15
Raw View
Hi Hankel,

Your understanding of the standard looks pretty good to me.

> That's strange. I thought programs (C)--(E) but not (B) should get compiled.
> Where does my misunderstanding lie?

You have two misunderstandings:

1.  MSVC++ 5.0 has not implemented the standard.  Therefore you can not
expect standard behavior out of it.

2.  MSVC++ 5.0 help files are not in sync with their headers.

MSVC++ 5.0 implements <cstdio> like this:  (after preprocessing)

#include <stdio.h>

Even if <cstdio> was implemented like their help file says, this would
still be non-standard because the following program should also compile:

#include <stdio.h>
int main() {
    std::printf("Hello, world!\n");
    return 0;
}

<stdio.h> is defined to promote std::printf to the global namespace.  But
that does not mean that you can no longer use std::printf.

If you require a standard conforming compiler in this respect, I submit a
shameless plug for CodeWarrior Pro3 by Metrowerks (available on Windows).
It should be available in mid April.

-Howard

In article <350954BE.61439858@ust.hk>, "Hankel O'Fung" <hkfung@ust.hk> wrote:

> Thanks Howard. However, I still in doubt about the use of headers and
namespaces.
>
> I am using MSVC++ 5.0. In her Help content, Microsoft claims that her <cstdio>
> was implemented in the following manner:
>
>     namespace std
>     {
>         #include <stdio.h>
>     }
>
> If I understand correctly, this means <cstdio> (our <cX>) is the
<stdio.h> (our
> <X.h>) wrapped in namespace std. However, in the following six programs,
only (A)
> and (B) can be compiled:
>
> // program (A) --- the classic "Hello, world" program in C
> #include <stdio.h>
> int main() {
>     printf("Hello, world!\n");
>     return 0;
> }
>
> // program (B)
> // Why could this be compiled???
> #include <cstdio>
> int main() {
>     printf("Hello, world!\n");
>     return 0;
> }
>
> // program (C)
> // error C2653: 'std' : is not a class or namespace name
> #include <cstdio>
> int main() {
>     std::printf("Hello, world!\n");
>     return 0;
> }
>
> // program (D)
> // error C2653: 'std' : is not a class or namespace name
> // error C2873: 'printf' : symbol cannot be used in a using declaration
> #include <cstdio>
> using std::printf;
> int main() {
>     printf("Hello, world!\n");
>     return 0;
> }
>
> // program (E)
> // error C2871: 'std' : does not exist or is not a namespace
> #include <cstdio>
> using namespace std;
> int main() {
>     printf("Hello, world!\n");
>     return 0;
> }
>
---
[ 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/03/17
Raw View
>"Hankel O'Fung" <hkfung@ust.hk> wrote:
>> I am still in doubt about the use of headers and namespaces.

For a thorough overview of what a conforming set of standard headers
might look like, see

  http://www.cantrip.org/cheaders.html

It describes in detail only one of several possible implementation
approaches, but the requirements list applies to any.  Note that
the page referenced above is *not* (yet) linked from my other pages,
so if you think it's interesting you should bookmark it separately.

--
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              ]





Author: "Hankel O'Fung" <hkfung@ust.hk>
Date: 1998/03/11
Raw View
In "An Introduction to Namespaces" (C/C++ Users Journal, January 1998),
Dan Saks says

"Headers in the C++ library, other than those from the C library, now
have names with no suffix at all. For example, the header that was once
<iostream.h> is now <iostream>. The committee (here Saks refers to the
C++ standards committee) also added a geader whose name has no .h suffix
for each header from the C library. For example, there is now a header
<cstring> that effectively replaces <string.h>. The headers whose names
end in .h have been deprecated, meaning they may disappear from a future
revision of the C++ standard."

"For reasons that escape me, the C++ Standards committee decided against
providing headers to support existing code. For example, the C++
Standard library does not provide a version of <iostream.h> that
declares names globally rather than in a namespace."

However, in p.431 of the 3rd edition (1997) of Stroustrup's "The C++
Programming Language",
we have

"For every header <cX> defining names in the std namespace, there is a
header <X.h> defining the same names in the global namespace.

So, what's the current situation now? If we denote the C headers in the
old days by <X.h> and those C++ but non-C counterparts by <Y.h>, am I
correct to say that the standard library now contains

        headers of the form <cX> and <Y> which declare names in
namespace std, and
        headers of the form <X.h> which declare names in the global
namespace,

but not headers of the form <Y.h> anymore? I am very confused!

Hankel
--
Dept of Ind Engg & Engg Mgt (IEEM)
HKUST, Clear Water Bay, Kowloon
Hong Kong

tel   (852) 2358 7103      fax   (852) 2358 0062
http://home.ust.hk/~hkfung

"For as long as I can remember, I've been searching for some reason why
we're here -- what are we doing here, who are we? If this is a chance to
find out even just a little part of that answer, I think it's worth a
human life, don't
you?"                                                   - Ellie Arroway
---
[ 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: sorry@but.spammed.out (Howard Hinnant)
Date: 1998/03/12
Raw View
In article <3506FD1F.F47FA0A6@ust.hk>, "Hankel O'Fung" <hkfung@ust.hk> wrote:

>
>         headers of the form <cX> and <Y> which declare names in
> namespace std, and
>         headers of the form <X.h> which declare names in the global
> namespace,
>
> but not headers of the form <Y.h> anymore? I am very confused!

You're not confused.  You have it right.  However, it is common for
implementations to provide <Y.h> which are analogous to the <X.h> in that
they promote their contents to the global namespace.  Note that this
non-standard extension fails for some of the headers because of conflicts
with <X.h>.  Specifically:  <limits.h>, <locale.h> and <string.h>.

-Howard
---
[ 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: "Hankel O'Fung" <hkfung@ust.hk>
Date: 1998/03/15
Raw View
Howard Hinnant wrote:

> In article <3506FD1F.F47FA0A6@ust.hk>, "Hankel O'Fung" <hkfung@ust.hk> wrote:
>
> >
> >         headers of the form <cX> and <Y> which declare names in
> > namespace std, and
> >         headers of the form <X.h> which declare names in the global
> > namespace,
> >
> > but not headers of the form <Y.h> anymore? I am very confused!
>
> You're not confused.  You have it right.  However, it is common for
> implementations to provide <Y.h> which are analogous to the <X.h> in that
> they promote their contents to the global namespace.  Note that this
> non-standard extension fails for some of the headers because of conflicts
> with <X.h>.  Specifically:  <limits.h>, <locale.h> and <string.h>.
>
> -Howard

Thanks Howard. However, I still in doubt about the use of headers and namespaces.

I am using MSVC++ 5.0. In her Help content, Microsoft claims that her <cstdio>
was implemented in the following manner:

    namespace std
    {
        #include <stdio.h>
    }

If I understand correctly, this means <cstdio> (our <cX>) is the <stdio.h> (our
<X.h>) wrapped in namespace std. However, in the following six programs, only (A)
and (B) can be compiled:

// program (A) --- the classic "Hello, world" program in C
#include <stdio.h>
int main() {
    printf("Hello, world!\n");
    return 0;
}

// program (B)
// Why could this be compiled???
#include <cstdio>
int main() {
    printf("Hello, world!\n");
    return 0;
}

// program (C)
// error C2653: 'std' : is not a class or namespace name
#include <cstdio>
int main() {
    std::printf("Hello, world!\n");
    return 0;
}

// program (D)
// error C2653: 'std' : is not a class or namespace name
// error C2873: 'printf' : symbol cannot be used in a using declaration
#include <cstdio>
using std::printf;
int main() {
    printf("Hello, world!\n");
    return 0;
}

// program (E)
// error C2871: 'std' : does not exist or is not a namespace
#include <cstdio>
using namespace std;
int main() {
    printf("Hello, world!\n");
    return 0;
}

That's strange. I thought programs (C)--(E) but not (B) should get compiled.
Where does my misunderstanding lie?

Hankel
http://home.ust.hk/~hkfung
---
[ 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              ]