Re: location of using, include, and namespaces statement
From: Anthony Borla (ajborla_at_bigpond.com)
Date: 12/13/03
- Next message: T250: "Base class pointer to sub class instance?"
- Previous message: Derek Baker: "Re: strings"
- In reply to: Arthur J. O'Dwyer: "Re: location of using, include, and namespaces statement"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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
- Next message: T250: "Base class pointer to sub class instance?"
- Previous message: Derek Baker: "Re: strings"
- In reply to: Arthur J. O'Dwyer: "Re: location of using, include, and namespaces statement"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|