Are there any issues with using namespace std ?

Hi everyone

I always use this in my programming, my friend said I shouldn't as it could cause issues, does anyone know of any issues it can cause?

Thanks :)
When you are using multiple libraries it can make things a bit more confusing.
See http://www.cplusplus.com/forum/beginner/15930/
Well; not really but it's good to be clear about where you're getting functions from. For example;
1
2
cout << something; // Where am I getting this "cout" statement from? Where do I look to find out what it does?
std::cout << somethiing_else; // cout is in the std namespace. I'll look in the STL to see what it does.  

It makes it clearer where you're getting things from. Really; any C++ programmer who doesn't know about cout is probably not a C++ programmer at all; but for other things it's useful.
The only place you should not ever use it is in a header file (or anything else #included by other people).
Suppose you have a custom string algorithm, trim(), in your global namespace. The next version of the C+ standard decides to includes a std::trim() in a new string algorithm library. One of your dependencies decides to include the new <string_algorithm> header. Which trim() gets called, yours or the std version? By always putting "using namespace std;" in your code, the answer may not be what you expect or intend.

Here is why my example is a real-world problem:

http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2006/n2059.html#trimming-and-padding

Namespace pollution is a very real issue. Be very clear with your namespaces. Avoid using namespace directives. If you must for clarity or brevity, use a using declaration (e.g. "using std::cout;"), but only in the scope in which you need it, preferably inside a function/method.

Never put using directives or declarations in headers. You will screw yourself and anyone who happens to include your header.
even if you put the using declaration in a cpp file name conflicts can still arise. count is a pretty common name and there is an algorithm called std::count. If you happen to have variables named count it can cause compiler errors depending on where you have put the using declaration. I do remember seeing a thread within the last year where someone had that problem.

If you want to save some typing for yourself use the using keyword for the most common things that you do so that it is more specific. That should eliminate the possibility of name conflicts while still saving you some typing.

1
2
3
using std::cout;
using std::cin;
using std::string; // and so forth 
Topic archived. No new replies allowed.