Advantages?

Started by
3 comments, last by vbuser1338 18 years, 9 months ago
Ok, I just bought the book Accelerated C++, and noticed that the author doesn't use the "using namespace std". I was just wondering what the advantages are of doing this when you will just have to type more. Thanks Whiz_Kid
--------------------------------A man of few words does not mean he does not have big ideas
Advertisement
It just helps to avoid namespace conflicts. That's basically it.
The main advantage is probably one of clarity. To my knowledge, the compiler doesn't really care either way so long as it's correct [smile]

If you're mixing/using a lot of namespaces, keeping their respective names tagged onto the front can make it (instantly) clear which namespace that symbol came from.

hth
Jack

<hr align="left" width="25%" />
Jack Hoxley <small>[</small><small> Forum FAQ | Revised FAQ | MVP Profile | Developer Journal ]</small>

"using namespace std" brings everything in the std namespace into scope. Often, this will bring things we don't even care about into scope. This is called namespace pollution. It can create issues like name collisions and is just sloppy. Look at it this way, one of the complaints about macros is that they ignore namespaces. You just told everything in std to feel free to ignore namespaces.

Now, there are two options. You can type "using std::foo" for everything you want to bring into scope, or you can type "std::foo" everywhere. The former is less typing overall and documents why you included a certain header. The latter is more typing overall, but gives the reader more information at the point of usage and you will never have a name collision ("using std::foo; using dts::foo;"). The former is more common, but I've heard arguments both ways from people who know what they're talking about.
I think this is because the main purpose for this is that namespaces are for saving names so you can have descriptive names and functions. If you just use a using statement that kindof rules out the point of the namespace. Although chances are it doesn't really matter if you use a using declaration because names you create probably won't clash. I think its mainly good coding style. This is only my view on it from what little of c++ I know so don't take my opinion on that is why its like that.

Cya,
vbuser

This topic is closed to new replies.

Advertisement