8

I am little confused as to which one is the better way to use using C++ keyword for namespaces. Suppose that the following code is in a header file backtrace.h

#include <memory>
using my_namespace1::component1;
using my_namespace2::component2;
namespace my_application {
  namespace platform_logs {
    class backtrace_log {
          //code that creates instances of my_namespace1::component1 and my_namespace2::component2
    };
  }
}

OR

#include <memory>    
namespace my_application {
  namespace platform_logs {
    using my_namespace1::component1;
    using my_namespace2::component2;

    class backtrace_log {
          //code that creates instances of my_namespace1::component1 and my_namespace2::component2
    };
  }
}

which one is better and why ?

4
  • It is a matter of taste & preference Commented May 25, 2017 at 17:22
  • @NicolBolas edited the question. Commented May 25, 2017 at 17:23
  • 5
    I'd advise against using deeply nested namespaces (where deep is greater than 1) - all they do is make your code hard to understand. The prime reason for using namespaces is to avoid name clashes - if you don't have such clashes, you don't need a complex namespace scheme. Commented May 25, 2017 at 17:23
  • 4
    I only use using statements in a cpp file. In a header file, I only use them inside inline functions (mostly template functions), and even then it's usually a namespace alias. Commented May 25, 2017 at 17:23

3 Answers 3

9

You should always pull in symbols from other namespaces in as narrow a scope as possible (to not pollute outer namespaces) and generally, only pull in the specific symbols you need, not just the entire namespace.

In headers (that may get included by many users) you should generally refrain from the practice alltogether and instead prefer to always just use explicit namespace qualifications on types.

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

1 Comment

Could you provide some resource for your admonitions? I appreciate your opinions on the topic but I would like some formal reference to examine. Thanks.
6

In a header file... the following example is evil. Never use using namespace... in the global scope of a header file. It forces a lot of unasked for symbols on the user that can cause very hard to fix problems if they clash with other headers.

#include <memory>
using my_namespace1::component1;
using my_namespace2::component2;
namespace my_application {
  namespace platform_logs {
    class backtrace_log {
          //code that creates instances of my_namespace1::component1 and my_namespace2::component2
    };
  }
}

On the other hand this next example is fine, but to be used with caution. It only makes sense if you want to include all those symbols in your API. This is often the case with internal project name spaces but less likely with third party namespaces.

I would especially avoid even this with very large namespaces like std::.

#include <memory>    
namespace my_application {
  namespace platform_logs {
    using my_namespace1::component1;
    using my_namespace2::component2;

    class backtrace_log {
          //code that creates instances of my_namespace1::component1 and my_namespace2::component2
    };
  }
}

Comments

2

In your first example whenever you include

   backtrace.h

in another file that file will also use those namesapces. So if by mistake someone wrote "blax" in a file where you included backtrace.h and "blax" also happened to be a class or function in you namespace, e.g.:

my_namespace1::component1::blax

the compiler might take his "blax" to mean "my_namespace1::component1::blax"

By placing the using namespace inside another namespace you simply include all those definitions into that namespace, in the previous example, if you went with version two of the code that collision wouldn't happen since "my_namespace1::component1::blax" would be included as my_application::platform_logs::blax.

Overall most coding guides would encourage you to: a) Prefer to not ever do "using namespace" or at least use it just to shorten (e.g. using short_namespace = first_namespace::another_namepsace::last_namespace)

b) If you do "using namespace" do so in the source file (i.e .cpp or .c files), since the definitions in those files are not included

c) If you do "using namespace" in headers nest it in another namespace (like in your example) so that it doesn't "leak" into the scope of other file which include your header

2 Comments

Could you substantiate "it might not depending on the linker used"?
@Jesper Juhl I was under the impression that including headers with a using statement is undefined behavior (e.g. if I include a.h and b.h, where b uses some functions that could only work assuming a had using namespace ... etc then depending on the linking order the code might or might no compile)... but now I'm searching around the internet and can't find example of this, so its likely that at the time of reading I did not understand linkers (not that I do now) and my mind twisted the statement. Thank you for pointing out, I will simply remove that.

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.