tags:

views:

222

answers:

5

Hi,

In c++ Is it OK to include same namespace twice? compiler wont give any error but still will it affect in anyway

Thanks,

EDIT:
I meant

using namespace std;

// . . STUFF
using namespace std;
+1  A: 

I'm not entirely sure what you mean. You can put multiple classes in a single namespace (that's the whole idea). Each class generally has it's own files, so yes, you can use the same namespace in multiple files.

Though not technically required, it's a good idea to have a directory structure that represents the namespace hierarchy you create.

As for for the using directive: the compiler and/or intellisense most likely gives you a warning (the C# one does), but otherwise there is no effect.

Thorarin
+1  A: 

Twice in the same class/file? It shouldn't be a problem, but neither should it be necessary. I'd naively assume that you should be able to refactor your code to avoid the issue.

RJFalconer
+11  A: 

It depends what you mean by 'include'. Saying:

using namespace std;    
...    
using namespace std:

is OK. But saying:

namespace X {
   ...
namespace X {

would create a nested namespace called X::X, which is probably not what you wanted.

anon
+1  A: 

Are you asking whether following is okay ?

using namespace std;
using namespace std;

It is okay but normally I prefer to refer the namespace element with scope resolution.

ex:

std::vector
std::sort
aJ
+5  A: 

This usage is fine, if it's what your talking about:

File: foo.h

namespace tools
{
  class Widget
  {
  ...
  };
}

file: bar.h

namespace tools
{
  class Gizmo
  {
  ...
  };
}
John Dibling