Re: location of using, include, and namespaces statement

From: Anthony Borla (ajborla_at_bigpond.com)
Date: 12/13/03


Date: Sat, 13 Dec 2003 02:47:39 GMT


"Arthur J. O'Dwyer" <ajo@nospam.andrew.cmu.edu> wrote in message
news:Pine.LNX.4.58-035.0312121943350.20842@unix40.andrew.cmu.edu...
>
> On Fri, 12 Dec 2003, Anthony Borla wrote:
> >
<SNIP>
> >
> > // tree.h ---------------------------------------
> > #ifndef tree_h
> > #define tree_h
> >
> > #include <ostream>
> > using std::ostream;
>
> Now, this is what I was unsure of earlier. Doesn't that 'using'
> directive now apply to the client's source file, too, if he ever
> #includes this header? That's a Bad Thing, right? because suppose
> the client didn't *want* to have his own global namespace polluted
> by 'ostream'?
>
> > namespace myNamespace
> > {
>
> And would it help or hurt to put the 'using' directives inside
> here?
>

No, your'e quite right - if namespace pollution is a concern [I suppose it
would be (though the severity of the problem would vary) in all but the
smallest programs like, say, student assignments] then best to expose
required names *within* the namespace body [though the headers should still
be placed outside of this, near the top of the file] so preventing the
problem you mention.

I totally avoid such issues by fully qualifying each name, particularly
those from 'std':

    std::cout << ...
    ...
    ... = std::strlen(...);

Any particularly lengthy namespace names can be aliased:

    namespace sht = very_long_namespace_name;
    ...
    sht::function(...);
    ...

and still be fully qualified. Needless to say, such practices should be
clearly documented :) !

>
> > // tree.cxx -----------------------------------------
> >
> > // *** Any headers need in *this* file
> > // ...
> > // ...
> >
> > // *** Headers included here can be assumed included
> > #include "tree.h"
>
> However, it's worth noting that you should never
> *assume* that the header file #includes, say, <string>,
> if the code in this file depends on <string>. Better safe
> than sorry. Especially if someone comes along and
> removes the #include <string> line from
> the header when you're not looking.
>

For absolute safety, I agree: make no assumptions, be explicit. Again,
though, in small projects I believe you can, usually, assume inclusion of
the headers for types used as data members, or function parameters and
return types.

>
> > void tree::print(ostream& out) const
> > {
> > out << name.c_str();
> > }
>
> And absolutely tangentially to everything, does the .c_str()
> here save you anything? Why not "out << name;" ?
>

This is an oversight - I should have removed it when pasting the code into
the message body.

Cheers,

Anthony Borla



Relevant Pages

  • Re: Whats the difference? string and std::string
    ... you should not use the directive in a header. ... >In a header you should avoid using directives (eg using namespace ... I agree that using declarations and using directives can be misused as ...
    (alt.comp.lang.learn.c-cpp)
  • Re: Flat File Problem
    ... It was the namespace I was using to ... When I debug the orchestration the RecCount variable is null. ... Have you deployed the Biztalk Assembly that contains the property schema ... And you can promote properties from the Header into the message context, ...
    (microsoft.public.biztalk.general)
  • Re: What is a header?
    ... >>A header is an inclusion at the beginning of your source file. ... A class definition, by itself, does not cause the compiler ... Any external class member function definitions ...
    (comp.lang.cpp)
  • Re: Utility to ensure appropriate headers were included
    ... failed to compile. ... This wasn't a problem on most systems because some of the other header ... a header file that's included directly in the source file? ... Your program that happens to use and that calls puts can ...
    (comp.lang.c)
  • Re: Splitting up code ?
    ... compile the first source file and it defines storage for the global ... The opposite situation is that header guards can sometimes cause a source ... functions and global variables. ...
    (comp.lang.c)

Loading