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.
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.
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);
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 ?
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.
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.
Comment