Click here to Skip to main content
15,888,527 members
Please Sign up or sign in to vote.
3.13/5 (4 votes)
See more:
Evening,
I was wondering what exactly is using namespace std?
and what are namespaces in general?
are they some kind of libraries or what?
Thanks.
Posted
Updated 27-Nov-13 7:31am
v2
Comments
Sergey Alexandrovich Kryukov 27-Nov-13 13:31pm    
What prevents you from reading C++ documentation? Why do you think that some Quick Answer created on the fly specially for you should be better then documentation or a well-though article written with dedication of decent time to proper explanation? No, namespaces are not libraries, they are nothing but the way to give declarations longer names to help better avoiding of name conflicts. Read about further detail by yourself.
—SA
Yasser El-Sayyed 27-Nov-13 13:34pm    
I did try to search for it and didn't fully understand what it meant, that's why i came here!
Sergey Alexandrovich Kryukov 27-Nov-13 13:38pm    
Impossible. Then you need to think at your searching and reading skills. I hope you got the answer. But you don't need to search, this is not a technique or a trick, this is a part of standard documentation and any C++ manual, any at all. Aren't you trying to convince us that you cannot get to any of the manuals, too? Sorry, but I cannot attribute it to anything except laziness.
Good luck.
—SA

A namespace is like a container for distinct names (originally from C). Essentially, when you use libraries of functions, you have the problem that common names may overlap. For example, if you have a library that executes the maximum value of a vector, it may be called max(). Well, since that name is so short and generic, there's a high possibility that some other library already has a function named max(), so it's "enclosed" in a namespace.

Your namespace can be called whatever you want. If you call it let's say "math_lib", then you can access your functions within that namespace by specifying the namespace then the function using the namespace::function notation (and yes, you can have several layers of this). As you might imagine, within a source file, you may use the functions within a namespace over and over again, and rather than having to specify the namespace every single time, you can use the using namespace directive, which essentially tells your compiler that you're using the specified namespace for the whole function, method, or file (depending on the scope of your statement).

So when you're using namespace std, you're literally using the namespace named std. Meaning you don't have to say std::vector (for example) every time you use a vector and can simply omit the std:: portion.

In summary, a namespace is made to contain a number of functions. Usually libraries use them to make sure their functions don't overlap with other functions having the same name/prototype. It was a bigger problem in C because of the lack of classes to act as containers (and because of the way prototypes were handled), but it's still around in C++ essentially for the same reason of creating containers.
 
Share this answer
 
Microsoft documentation explains Namespace.

Namespace[^]

Using Namespaces[^]

Namespace statement[^]
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 27-Nov-13 13:39pm    
5ed.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900