6

I understood, that I should not use this in a header file:

using namespace foo;

Because it brings the namespace foo in the global scope for anyone who uses my header file.

Can I prevent this from happening, if I do it in my own namespace? For example like this:

namespace my_lib
{
    using namespace foo;

    // my stuff
    // ...
}

Now the using namespace foo should be restricted to the scope of the namespace my_lib, right?

DyingSoul
  • 231
  • 5
  • 11
  • Be aware that that drags the entire contents of namespace `foo` into namespace `my_lib`. Is this *really* what you want for your users? It may be better than polluting the global namespace, but you're still polluting *a* namespace... – ildjarn May 03 '11 at 11:45
  • 2
    Are you just looking to shortened a namespace? rather than `foo::bar::box::action()` you want `my_lib::action()` In this situation what you want is namespace alias `namespace my_lib = foo::bar::box;` – Martin York May 03 '11 at 14:46

2 Answers2

9

Yes. That is better than using using namespace foo at global level.

Yet better would be if you use foo::name syntax.

Now the using namespace foo should be restricted to the scope of the namespace my_lib, right?

Yes. It brings all the names from the namespace foo in the namespace my_lib which might cause name collisions in my_lib. That is why foo::name is the most preferrable approach.

Nawaz
  • 353,942
  • 115
  • 666
  • 851
  • 2
    I disagree that always using the full namespace name is appropriate. Aliasing and bringing names in scopes greatly simplifies the code. If users of `my_lib` would almost always need to use `foo` as well it makes sense to combine their scope. – edA-qa mort-ora-y May 03 '11 at 12:14
  • @edA-qa mort-ora-y: What if there is name collisions? Also, `my_lib` might refer to other names from other dozen namespaces, so does it mean that it should use `using namespace xyz` for all namespaces? – Nawaz May 03 '11 at 12:18
  • I'm also in favour of bringing names into scope when it makes the resulting code easier to read. There are ways to achieve it that don't have any major downsides, however (e.g. see below). – Stuart Golodetz May 03 '11 at 12:20
  • collisions are always possible, but I don't see any reason why their possibility should prevent you from importing names. If one namespace is intended to be layered on another you'll just have to avoid collisions. If two namespaces are completely unrelated then the importing of names probably shouldn't be done. – edA-qa mort-ora-y May 03 '11 at 12:56
  • @edA-qa mort-ora-y: Your comment doesn't really address the issues of name collisions. As I said what if name collisions occur? Also, bringing multiple names that also includes functions into the scope might create problem ADL and other name lookup, that in might produced unintended function resolution, and so on! – Nawaz May 03 '11 at 14:07
1

Yes, if you do that then it just brings all the names from foo into my_lib -- which, as others have pointed out, may or may not be desirable.

One thing I would observe in addition to what others have said is that you can use the 'using directive within a namespace' idea as a way of simulating a using directive that is restricted to class scope. Note that this is illegal:

class C
{
  using namespace boost; // for example

  // ...
};

But you can do this instead:

namespace C_Namespace {
  using namespace boost;

  class C
  {
  };
}

using C_Namespace::C; // bring C itself back into the global namespace

Just thought you might find it useful if what you really want is to be able to define something (like a class) without writing a particular namespace prefix the whole time.

Stuart Golodetz
  • 20,238
  • 4
  • 51
  • 80