It depends on where you are using it and for what reason. There is nothing inherently evil about
using namespace std; --except when you contaminate someone else's code with a foreign namespace.
For example, the following main program is perfectly valid:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
|
#include <ciso646>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include "addn.hpp"
using namespace std;
using namespace addn;
int main()
{
vector <int> xs;
string s;
cout << "Please enter some numbers. Press Enter twice to stop.\n";
while (getline( cin, s ) and !s.empty())
{
istringstream iss( s );
int x;
while (iss >> x)
xs.push_back( x );
}
cout << "The sum of the numbers is " << add( xs ) << endl;
return 0;
}
|
It is fine because, as the program author, you are perfectly within your rights to put whatever you want into your global namespace (or any other namespace you use).
However, when writing libraries and include files, do
not assume that you have any right to fill the user's namespace with anything. Here's the rest of the example, to demonstrate proper namespace encapsulation:
addn.hpp
1 2 3 4 5 6 7 8 9 10 11
|
#ifndef ADDN_HPP
#define ADDN_HPP
#include <vector>
namespace addn
{
int add( const std::vector <int> & xs );
}
#endif
|
addn.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14
|
#include "addn.hpp"
namespace addn
{
using namespace std;
int add( const vector <int> & xs )
{
int result = 0;
for (vector <int> ::const_iterator xi = xs.begin(); xi != xs.end(); xi++)
result += *xi;
return result;
}
}
|
Notice how the #include file does
not have a using namespace anything. The person who includes the file may not want stuff from the standard namespace in their global namespace. However, in the library file, it was perfectly fine to do it, since it is compiled separately and cannot contaminate anything.
One more thing to note: keep
everything you write in a library file inside a namespace. This prevents namespace collisions between libraries when linking.
Hope this helps.