Right usage of namespace std.

I want to know where to use the namespace std. What is it and what is it's use? Where should it be used?
namespace std makes it so you don't have to put std:: in front of members of std.
example:
1
2
3
4
5
6
#include <iostream>

int main()
{
     std::cout<<"Hello World";
}

vs
1
2
3
4
5
6
7
#include <iostream>

using namespace std;

int main() {
     cout<<"Hello World";
}

std is needed for strings to.
What is it and what is it's use?
using namespace std; dumps everything in namespace std into global namespace. Its intended use was to allow easy compatibility for old pre-namespace programs.

Where should it be used?
Almost never. If you have good reasons, better use it in the least possible scope. Even than prefer bringing concrete entities as opposed to everything. But NEVER use it in headers.

Main problem with using namespace std; is that it brings everything in global scope. And standard library uses many names you probably can use in your code. This often leads to errors when used in something other than 100-lines school assigments. To make matter worse, you might only run into poblems when you need to include another standard header, or switch compilers, or even update your compiler.

http://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice
Topic archived. No new replies allowed.