12

I've been told it's bad to have "using namespace ns123" in a header file, but I can't remember what the reason given was. Is it in fact a bad thing to do, and why?

2
  • 4
    You left it unstated but I believe you meant (and all of the current answers assume) that you're talking about using directives at global scope in a header. In non-global scopes (e.g. in namespace over which you exercise complete control or inside a function) you'll get a different answer. Commented Feb 9, 2010 at 21:21
  • This question would be better if it addressed the concern raised by Roger Pate. Commented Aug 6, 2016 at 13:41

5 Answers 5

26

It's a bad practice, in general, because it defeats the purpose of namespaces. By defining in a header you're not enforcing strict control over the scope of the using declaration, meaning that you can run into name clashes in unexpected places.

Sign up to request clarification or add additional context in comments.

1 Comment

Are you talking about directives or declarations? Or both?
18

If you put a using declaration in a header file, anything that #includes the header file also has the namespace imported, whether they want it or not. This violates the principle of least surprise and defeats the purpose of namespaces by allowing changing an #include statement to easily cause a naming collision. If you want to import a namespace in your own .cpp file to save a little typing and produce more readable code, that's fine. Just don't force users of your module to do the same.

1 Comment

using namespace N; is a using directive, not a using declaration. A using declaration may be appropriate, as it only affects the specified name instead of anything and everything in N, if that is what is desired. (using N::name; is an example using declaration.)
3

Pulling all the classes and functions out of their namespace is generally a bad idea and is actually opposed to concept of having namespaces...it is usually better to use 'using' on a specific class or function.

1 Comment

It's not as bad in a .cpp file, though, since you're ignoring namespaces in a local area. In an .h file, you're ignoring namespaces in a large and not well-defined area.
1

It will often give you strange and mysterious compilation errors, for one thing, where you can spend hours trying to figure out the root of the problem.

Comments

1

Ordinary C++ Programming

Yes there are often places where this makes sense, and you have control over where and how you use your using stamenets. If this is a program with a limited scope you do not need to worry about place usings in global scope.

For monolithic applications I would strongly recommend not placing usings in a very busy namespace -- as collisions will drive you mad once you are committed. For example:

  1. ::
  2. ::companyname
  3. ::companyname::component
  4. ::companyname::component::sub-component.

I would say that it is perfectly acceptable to start place using statements at level 3 or 4 -- thats where the chances of collisions start becoming very low.

Generic / TMP Programming

In generic programming and TMP using is often used to configure your libraries for a specific domain and this is often done through using statements.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.