-4

Possible Duplicate:
Why is 'using namespace std;' considered a bad practice in C++?

I have been told before that there is some dangers in using using namespace ...; in C++ as it litters the global namespace or something. Is this true and are there any real dangers?

Community
  • 1
  • 1
Cole Tobin
  • 9,206
  • 15
  • 49
  • 74

2 Answers2

3

Dangers are only if use using namespace ... in headers. Because headers may be included anywhere.

ForEveR
  • 55,233
  • 2
  • 119
  • 133
  • 4
    And in the global namespace of a .cpp file. Because the name lookup rules are complex enough already. – James McNellis Jul 18 '12 at 22:42
  • @JamesMcNellis: I actually find it more confusing when the using directive is included in a deeply nested namespace... it can have even more surprising effects than in employing it in the global namespace (that is, in the global namespace the identifiers from the used namespace at least reside in the same location than the using directive...) – David Rodríguez - dribeas Jul 18 '12 at 23:25
1

Yes it is true. There is a lot of documentation on the web on this issue. This is because it makes available to the global namespaces names that could very well be used by yourself later. For example, if you include the std namespace and later try to write a max() or min() function, you will most likely get compiler errors.

ApplePie
  • 8,814
  • 5
  • 39
  • 60