Using Namespaces

Nov 24, 2008 at 2:38pm
Are there any benefits to using:

using namespace std;

rather than individual using statements?:

using std::cout;
using std::cin;
using std::endl;

Thanks,
Return 0;
Nov 24, 2008 at 3:28pm
It only takes one line. ;)
Nov 24, 2008 at 4:49pm
if you are using namespace std you will have the entire std namespace in the global scope.
By using just some elements you only have those elements of the namespace in you global scope.
If you are #including only standard headers you won't have problems but if you #include a header containing declarations of something which has the same name of an element from the namespace std and you are using the entire namespace you could get ambiguity errors.
Nov 24, 2008 at 6:37pm
I see. So it is probably better if I stick with using just the elements I need to avoid chasing down errors as I will be using headers from OpenGL and Ogre3d in the near future. Thanks.
Nov 24, 2008 at 8:53pm
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.
Nov 25, 2008 at 12:02am
That makes sense. Thanks.
Topic archived. No new replies allowed.