468,113 Members | 2,149 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,113 developers. It's quick & easy.

why use namespace std?

If we are having global namespace then why we are writing
using namespace std;
in c++?
Apr 8 '13 #1
8 5395
PreethiGowri
126 64KB
have a look at this site you'll understand better
Apr 8 '13 #2
weaknessforcats
9,207 Expert Mod 8TB
The namespace is to prevent your variable names from duplicating variable names used in code you include in your program.

This is a big problem in C. Once I had a working program and someone wanted to add Microsoft sockets. In the Microsoft code was a thing called GROUP which just happened to duplicate a thing in my code called GROUP. Now the program wouldn't even compile.

The result was I had to change several thousand lines of my code to take out GROUP and replace it with another name and hope that there would be no future clashes.

C++ uses a namespace to add a qualifier to your name making your name unique. So cout is really std::cout and cin is really std::cin.

When I say "using namespace std" I am really telling the compiler than when I say cout I mean my own cout or the one in std and I am not saying which one. Not good.

So, "using namespace std" means there is a tad bit of sloppy in the code. Correctly, cout in the std namespace should be in the code as std::cout.

BTW: This all means that an of your global variables shoud be inside your own namespace so their names don't conflict with anyone else's.
Apr 8 '13 #3
thank you weaknessforcats....
Apr 22 '13 #4
Sherin
77 64KB
A namespace is designed to overcome this difficulty and is used as additional information to differentiate similar functions, classes, variables etc. with the same name available in different libraries. Using namespace, you can define the context in which names are defined. In essence, a namespace defines a scope.
Jan 27 '21 #5
SwissProgrammer
206 128KB
Thank you weaknessforcats. Nicely explained, logic, and why, and why not.


In case the questioner needs an example:

Here is an example.

namespace std is a HUGE file


If someone will be using a lot of std like
using std::string;
using std::cin;
using std::cout;
using std::to_string;
using std::wstring;
using std::vector;

but if they have many many more, then some people use
using namespace std;
because it is handy.

But the namespace std is a HUGE file.

I try to not use it. I just list what I need and only use that part.

Or, I skip that part and put it directly into the code similar to this:
std::string NumberToString(int, double, long, long long int);
2 Weeks Ago #6
weaknessforcats
9,207 Expert Mod 8TB
A namespace is not a file. It is nomenclature to prevent duplicate function and variable names when you import someone else's code into your code. Or when your code is used by someone else.

Which cout is this:

cout << var;

The sytem cout ?
A cout overload you wrote ?
A cout you got from a vendor ?

The using directive permits ambiguity. Bad news.

Whereas:

std::cout << var;

is the system cout. No question.
2 Weeks Ago #7
SwissProgrammer
206 128KB
Thank you weakness for cats for correcting me by pointing out that namespace is not a file.

I am still learning C and C++ and even though I am trying to get it right I might have a long way go to reach your level.

I was calling it a file, but I think that it makes my program larger since I try to compile my small programs to one file without dlls. So, not as a namespace file, but how you said.

Thank you.
2 Weeks Ago #8
weaknessforcats
9,207 Expert Mod 8TB
I am changing the subject to the size of your compiled programs.

Only the functions you call are in your object program.

So if you use a library (.lib) that has 12,000 functions and is 2GB in size , your object program will contain only the bytes for the functions you call.

The linkers job is to copy the functions you call from the library to your object program. Then it links your call to the copy.

In the case of a dynamic library (.dll) only the link to the function you call is loaded. No part of the dll is loaded into your program space.

Star a new thread to keep conversation active.
2 Weeks Ago #9

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

1 post views Thread by John L. Clark | last post: by
25 posts views Thread by kj | last post: by
3 posts views Thread by Mike Dickens | last post: by
6 posts views Thread by Don Wash | last post: by
2 posts views Thread by puzzlecracker | last post: by
2 posts views Thread by Tony Johansson | last post: by
7 posts views Thread by Kevin Newman | last post: by
14 posts views Thread by Tiraman | last post: by
4 posts views Thread by Kevin Newman | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.