Topic: Standard header files inclusion of other standard header files


Author: "Pavel Kuznetsov" <su41vo7la001@sneakemail.com>
Date: Fri, 26 Oct 2001 07:17:55 GMT
Raw View
Terence Kelling wrote:
> I was thinking in "C" where that
> standard allows you to forward declare a library function [...].
> As I now understand it, this is disallowed in standard C++.


Phil Edwards wrote:
> Well.... I don't think it's prohibited.[*]  I think it's just one of those
> things that would fall under "bad practice, asking for trouble".
> [*]  I have not checked and could be wrong.

Actually it is. At least 17.4.3.1/1 [lib.reserved.names] says:
``It is undefined for a C++ program to add declarations or definitions
  to namespace std or namespaces within namespace std unless otherwise
  specified.''

Phil Edwards wrote:
> Look at the contents of the <iosfwd> header, for example. [...]
> There's no reason why you can't type similar forward declarations.

There is. Implementation could place everything it needs in namespace
other than std and then bring everything The Standard requires into
namespace std with using directives.

--
Pavel Kuznetsov

[ THE REPLY ADDRESS IS NOT GUARANTEED TO LIVE LONGER THAN FEW WEEKS! ]
[ The permanent address is: pavel <dot> kuznetsov <at> mail <dot> ru ]


---
[ 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: cmd@gmrc.gecm.com (Chris Dearlove)
Date: Fri, 26 Oct 2001 17:13:40 GMT
Raw View
Pavel Kuznetsov (su41vo7la001@sneakemail.com) wrote:
: There is. Implementation could place everything it needs in namespace
: other than std and then bring everything The Standard requires into
: namespace std with using directives.

If this is so could this mechanism be used by an implementor to
create header files that are (apparently) not polluted by other
header files (an area where C++ has - up until five minutes
ago I thought necessarily, I'm now not sure - taken a backward
step from C).

---
[ 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: comeau@panix.com (Greg Comeau)
Date: Sat, 27 Oct 2001 02:11:25 GMT
Raw View
In article <c0696667.0110251337.4dc00c16@posting.google.com>,
Terence Kelling <terencekelling@cs.com> wrote:
>comeau@panix.com (Greg Comeau) wrote in message news:<9r7tpm$n8f$1@panix3.panix.com>...
>> He just means that to use say std::string you should
>> #include <string>
>
>Ah, I see where my confusion lied.  I was thinking in "C" where that
>standard allows you to forward declare a library function and
>therefore not have to include its associated header.

As I recall it, C only allows you to do this for some functions,
not all of them.

>As I now
>understand it, this is disallowed in standard C++.  Thanks for the
>clarification.

Pretty sure you're right.
--
Greg Comeau         export ETA: December     See our Oct 31st special
Comeau C/C++ ONLINE ==>     http://www.comeaucomputing.com/tryitout
World Class Compilers:  Breathtaking C++, Amazing C99, Fabulous C90.
Comeau C/C++ with Dinkumware's Libraries... Have you tried it?

---
[ 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 Dennett <jdennett@acm.org>
Date: Sat, 27 Oct 2001 12:32:18 GMT
Raw View
Pavel Kuznetsov wrote:

> Terence Kelling wrote:
>
>>I was thinking in "C" where that
>>standard allows you to forward declare a library function [...].
>>As I now understand it, this is disallowed in standard C++.
>>
>
>
> Phil Edwards wrote:
>
>>Well.... I don't think it's prohibited.[*]  I think it's just one of those
>>things that would fall under "bad practice, asking for trouble".
>>[*]  I have not checked and could be wrong.
>>
>
> Actually it is. At least 17.4.3.1/1 [lib.reserved.names] says:
> ``It is undefined for a C++ program to add declarations or definitions
>   to namespace std or namespaces within namespace std unless otherwise
>   specified.''
>
> Phil Edwards wrote:
>
>>Look at the contents of the <iosfwd> header, for example. [...]
>>There's no reason why you can't type similar forward declarations.
>>
>
> There is. Implementation could place everything it needs in namespace
> other than std and then bring everything The Standard requires into
> namespace std with using directives.


Not quite everything; templates in namespace std can
be specialized for user-defined types, and that isn't
legal if they're not defined in namespace std.  There's
a similar minor issue which means that namespace std
can't be an alias for a namespace (e.g.) std_199711,
because specializations can't be defined in a namespace
alias the same way they can in the real namespace.

Next, Koenig lookup makes it seemingly important that
most if not all of the standard library must be defined
in the same namespace.  So it appears that there's not
much, if anything, of the public interface of ::std that
can be defined elsewhere and pulled in with using
declarations etc.

-- James Dennett

---
[ 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: "Richard Smith" <richard@ex-parrot.com>
Date: Tue, 30 Oct 2001 18:17:55 GMT
Raw View
"Phil Edwards" <pedwards@dmapub.dma.org> wrote in message
news:9ra81h$db6$1@dmapub.dma.org...
>
> Terence Kelling <terencekelling@cs.com> wrote:
> > comeau@panix.com (Greg Comeau) wrote in message
news:<9r7tpm$n8f$1@panix3.panix.com>...
> > >
> > > He just means that to use say std::string you should
> > > #include <string>
> >
> > Ah, I see where my confusion lied.  I was thinking in "C" where that
> > standard allows you to forward declare a library function and
> > therefore not have to include its associated header. As I now
> > understand it, this is disallowed in standard C++.
>
> Well.... I don't think it's prohibited.[*]  I think it's just one of those
> things that would fall under "bad practice, asking for trouble".
>
> Look at the contents of the <iosfwd> header, for example.  It's there just
> to forward declare a whole bunch of useful things, so that you and I don't
> have to include the "real" headers (which take more time to compile).
> There's no reason why you can't type similar forward declarations.
> But doing so can be very tricky, which is why it's not recommended for
> the standard library entities.

But even if you try to forward declare, say, std::string in some header file
by writting something like...

    namespace std
    {
      template <class CharT> struct char_traits;
      template <> struct char_traits<char>;
      template <class Type> class allocator;
      template <class CharT, class Traits = char_traits<CharT>,
                class Alloc = allocator<CharT> > class basic_string;
      typedef basic_string<char> string;
    }

... it won't work because as soon as someone includes both this header and
<string> in the same compilation unit you'll get an error about redefining
default template arguments.  (I must admit, I've never been very sure why
you're not allowed to redefine template arguments.)  So I'm not sure that
it's even possible if default template arguments are involved.

--
Richard Smith




---
[ 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: terencekelling@cs.com (Terence Kelling)
Date: Wed, 24 Oct 2001 19:04:37 GMT
Raw View
If a standard header file needs a function from another standard
header file, to be comformant, must it include the entire header file.
 I am unsure what Stroustrup meant when he wrote in "The C++
Programming Language", "For a standard library facility to be used its
header must be included."  For example, could an implementation have
some file named "swap.h" so that its own header files could just
include that file if it needed the swap function as opposed to having
to include all of <algorithm>?  And if an implementation must do so,
why?

Thanks,
Terence Kelling

---
[ 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: pedwards@dmapub.dma.org (Phil Edwards)
Date: Wed, 24 Oct 2001 19:14:55 GMT
Raw View
Terence Kelling <terencekelling@cs.com> wrote:
> If a standard header file needs a function from another standard
> header file, to be comformant, must it include the entire header file.

The Standard doesn't say anything about /how/ it happens.  It doesn't even
require that they be implemented as "files" (whatever those may mean for
any given system).  It only says that when you have

    #include <foo>

in your code, certain facilities are made available to you.  Everything else
is negotiable.


> For example, could an implementation have
> some file named "swap.h" so that its own header files could just
> include that file if it needed the swap function as opposed to having
> to include all of <algorithm>?

Of course.  Quite a few do.


Luck++;
Phil

--
If ye love wealth greater than liberty, the tranquility of servitude greater
than the animating contest for freedom, go home and leave us in peace.  We seek
not your counsel, nor your arms.  Crouch down and lick the hand that feeds you;
and may posterity forget that ye were our countrymen.            - Samuel Adams

---
[ 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: comeau@panix.com (Greg Comeau)
Date: Thu, 25 Oct 2001 05:52:18 CST
Raw View
In article <c0696667.0110240944.316a9dce@posting.google.com>,
Terence Kelling <terencekelling@cs.com> wrote:
>If a standard header file needs a function from another standard
>header file, to be comformant, must it include the entire header file.

A header from Standard C++ may #include another header.
Exactly how that happens is mostly up to the implementation.

> I am unsure what Stroustrup meant when he wrote in "The C++
>Programming Language", "For a standard library facility to be used its
>header must be included."

He just means that to use say std::string you should
#include <string>

Now, it may be so that you can use std::string by say
#include'ing <iostream>, which may have internally
#include'd <string>

>For example, could an implementation have
>some file named "swap.h" so that its own header files could just
>include that file if it needed the swap function as opposed to having
>to include all of <algorithm>?

Sure.   The Standard lays out the minimally required headers.
An implementation can certainly provide others, but they are
considered extensions, and all that that might mean.

>And if an implementation must do so, why?

Must do what?  As mentioned, Standard C++ lays out which
header names it supports, so a comforming implementation needs
to provide at least those.
--
Greg Comeau         export ETA: December     See our Oct 31st special
Comeau C/C++ ONLINE ==>     http://www.comeaucomputing.com/tryitout
World Class Compilers:  Breathtaking C++, Amazing C99, Fabulous C90.
Comeau C/C++ with Dinkumware's Libraries... Have you tried it?

---
[ 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: terencekelling@cs.com (Terence Kelling)
Date: Thu, 25 Oct 2001 21:39:15 GMT
Raw View
comeau@panix.com (Greg Comeau) wrote in message news:<9r7tpm$n8f$1@panix3.panix.com>...
>
> He just means that to use say std::string you should
> #include <string>
>

Ah, I see where my confusion lied.  I was thinking in "C" where that
standard allows you to forward declare a library function and
therefore not have to include its associated header. As I now
understand it, this is disallowed in standard C++.  Thanks for the
clarification.

Terence Kelling

---
[ 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: pedwards@dmapub.dma.org (Phil Edwards)
Date: Thu, 25 Oct 2001 23:58:31 GMT
Raw View
Terence Kelling <terencekelling@cs.com> wrote:
> comeau@panix.com (Greg Comeau) wrote in message news:<9r7tpm$n8f$1@panix3.panix.com>...
> >
> > He just means that to use say std::string you should
> > #include <string>
>
> Ah, I see where my confusion lied.  I was thinking in "C" where that
> standard allows you to forward declare a library function and
> therefore not have to include its associated header. As I now
> understand it, this is disallowed in standard C++.

Well.... I don't think it's prohibited.[*]  I think it's just one of those
things that would fall under "bad practice, asking for trouble".

Look at the contents of the <iosfwd> header, for example.  It's there just
to forward declare a whole bunch of useful things, so that you and I don't
have to include the "real" headers (which take more time to compile).
There's no reason why you can't type similar forward declarations.
But doing so can be very tricky, which is why it's not recommended for
the standard library entities.

For the string class, for example, the forward declaration is not just
"class string;" even though that's what the class looks like to us.
It's some horrifically long name with lots of default template parameters.
(Okay, three of them.  That's a lot. *grin*)

[*]  I have not checked and could be wrong.

Luck++;
Phil

--
If ye love wealth greater than liberty, the tranquility of servitude greater
than the animating contest for freedom, go home and leave us in peace.  We seek
not your counsel, nor your arms.  Crouch down and lick the hand that feeds you;
and may posterity forget that ye were our countrymen.            - Samuel Adams

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