Sign in to follow this  
whiz_kid

Advantages?

Recommended Posts

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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
"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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this