Monku
Monku

Reputation: 2650

How to use "using" keyword in C++

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 ?

Upvotes: 8

Views: 6877

Answers (3)

George
George

Reputation: 4011

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

Upvotes: 2

Galik
Galik

Reputation: 48625

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
    };
  }
}

Upvotes: 6

Jesper Juhl
Jesper Juhl

Reputation: 31475

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.

Upvotes: 9

Related Questions