Home

C++ Namespaces

2025-03-07

A brief overview of C++ namespaces and when you may want to use them.

Intro


A friend of mine who is learning C++ recently asked me how namespaces work and in what situations you may want to use them. Here is my explanation and recommendations for maintainable code.

What Are Namespaces?


A namespace is a declarative region that encompasses data and functions, providing them with a unique scope. You can define a namespace via the namespace keyword.

namespace emulator {
	CPU cpu;
};

To access cpu, you must use the scope resolution operator (::) along with the namespace i.e. emulator::cpu.

It is important to note that all data and functions inside a namespace are bound to that particular scope, so you can have several namespaces which contain identifiers of the same name. Consider the following code.

namespace A {
	int x = 5;
};

namespace B {
	int x = 8;
};

A::x and B::x exist independently of each other. As they have different scopes, they do not violate the One Definition Rule.

Unlike structs and classes, namespaces cannot be instantiated.

Why use Namespaces?


Namespaces are commonly used for grouping together related data and functions and abstracting them into a common namespace. Namespaces have several practical uses, such as cleaning the global scope, but they are mostly used for abstraction and organization.

The Global Scope

If you have ever programmed in C (or C++), you have most likely heard phrase “pollute the global scope.” The global scope encompasses all global variables, functions, or classes, hence the name.

Consider the following code.

int x;

namespace A {
	int y;
};

x belongs to the global scope whereas y belongs to the namespace A. Another example of data belonging to the global scope is int from stdint.h.

C does not have namespaces which is why you will hear programmers talking about how polluted its global scope is. It is beneficial to use namespaces in C++ because you can hide data from the global scope and free more identifiers for global/local variables. More than that, it makes your code more readable and maintainable because you can abstract and categorize related data and logic into different groups.

Using namespace std

Something beginner C++ programmers always hear is to not use using namespace std. This recommendation is completely valid, but many people do not understand why.

First, we need to understand what the using keyword is. In the context of namespaces, using imports the provided namespace into the global scope. What this means is that you can access data and functions from that namespace without having to use the scope resolution operator. You may think this is more efficient, but consider the following code.

namespace A {
	void func() {};
};

namespace B {
	void func() {};
};

using namespace A;
using namespace B;

func(); // conflict

Both A and B contain a method named func(). When you import both namespaces into the global scope, there is a conflict because func() is defined in both A and B, and the compiler does not know which function definition to use.

The reason why you should not use using namespace std is because you will receive a conflict like the one above if you have a global identifier or definition that coincides with something from std.

At the end of the day, by using using namespace, you end up reducing code readability and maintainability, and by “un-abstracting” all of the embedded data and functions, you are simply not using namespaces for what they were designed for.

Home