Role of namespace std in a program

Hello

My question is that what exactly does using namespace std; do in a program?

Thanks
From the C++ Standard

2 A using-directive specifies that the names in the nominated namespace can be used in the scope in which the using-directive appears after the using-directive. During unqualified name lookup (3.4.1), the names appear as if they were declared in the nearest enclosing namespace which contains both the using-directive and the nominated namespace. [ Note: In this context, “contains” means “contains directly or indirectly”. —end note ]
basically, it eliminates having to type std::. For example you may have seen codes that say std::cout<<"Hello World!" std::<<endl; However, when you include the namespace std, you can simply type cout<<"Hello World!" <<endl;
@vlad, where can i find the (C++ standard) specification ?

an easy explanation about namespaces:
http://www.cplusplus.com/doc/tutorial/namespaces/

everything in standard C++ is contained in the std namespace, so if you want to use any standard identifier without the scope resolution operator, you should tell the compiler to look for identifiers in the std namespace, like this:
using namespace std;
@Vlad

Thanks for explaining, but I didn't quite understand what you said. All I could conclude was that using the namespace std eliminates me to use std::cout and others in my program. So what's happening?

Thanks
The thing is, one shouldn't have using namespace std; in their code.

It pollutes the global namespace, and can cause naming conflicts. Did you know there are std::distance, std::left, std::right plus heaps of others that could cause awkward problems.

So if one had a function called distance there would be a problem, as std::distance means a very different thing.

One can do this for frequently used things, put them at the top of your file :

1
2
3
4
using std::cout;
using std::cin;
using std::endl;
using std::string;


Then refer to them without qualification :

1
2
3
string MyName = "Fred";

cout << "My Name is " << MyName << endl;


Some people use std:: exclusively.

It is a good idea to put your own code in it's own namepace.

So this is why you see experienced coders put std:: everywhere

HTH

@Rechard3

@vlad, where can i find the (C++ standard) specification ?


You can download the Draft of the Standard here

http://www.open-std.org/jtc1/sc22/wg21/
Topic archived. No new replies allowed.