Namespaces in C++

Amritanshu Verma
3 min readJan 6, 2021

When I started competitive coding, a friend of mine asked me to use

using namespace std

before the main code. I started doing that, without actually understanding what it really does. Oh! I knew one thing though. If we do not use that line, we have to write std::cout, std::string etc. So, I followed the template and saved myself from the trouble of that extra writing.

But C++ is awesome, and so are namespaces. So, let us explore this wonderful feature which C++ offers us.

What are namespaces ?

Namespaces are declarative regions for logical groups. Now, I know this may sound complex! So, let me simplify it a little bit.

If we have a set of functions, types and other variables which belong together, we can declare them together under one namespace. This also helps prevent name collisions, like, so,

In the code above, we can access the two print functions as A::print() and B::print() .

How to access members of a namespace

Let us say we want to access the print function of namespace A.

It can be accessed in two ways :

1. Using the fully qualified name of the function

2. Using the directive

When we use the fully qualified name, we can be sure of what function we are exactly using. But if we use the directive, there can be some ambiguity. If there is a local variable which coincides with any namespace variable, the namespace variable is hidden whereas if a global variable coincides with a namespace variable, the compiler gets confused on which one is to be used, thus throwing an error. Needless to say, we should avoid using using in the header .h files.

It is not necessary that the entire namespace sits at one place. It can be in distributed amongst files.

We have our very own std as an example. All the standard types and functions are a part of std. std::vector in vector.h and std::string in string.h are both part of std, even been belonging to different files.

When a namespace is discontiguous, all the parts of the namespace are combined by the preprocessor before execution.

What about the declarations without any namespace ?

The functions and types which are not a part of any namespace are by default part of a global namespace. This is generally not a good practice as this may lead to ambiguity.

Nested namespaces

Now, this was inevitable. We grew up reading nesting. Nested ifs, nested for loops, nested while loops, nested classes, nested functions etc. So, this was bound to come. Yes, we do have nested namespaces.

A namespace can be nested. A child namespace has access to all its parent’s members whereas a parent can only access its child’s members using fully qualified names.

However if the child namespace is an inline member, it can be used directly without the full qualifying name

This interesting property of namespaces can be used in versioning. We can expose the most recent version as an inline member which would be invoked if no qualifier is used.

Hope this would have given an insight into namespace :)

--

--