5 Common reasons of using namespaces in C++ projects

Namespaces were introduced to the C++ Standard in 1995 and usually they are defined like this:

A namespace defines a new scope. They provide a way to avoid name collisions.

Namespaces in C++ are most often used to avoid naming collisions. Although namespaces are used extensively in recent C++ code, most older code does not use this facility. 

After exploring the source code of many C++ projects, here are some common reasons of using the namespaces in these projects.

1- Avoid name collisions.

As speicified before it’s the common reason, in this case their use are just useful for the compiler, no added value for the developer to make the code more readable and maintanable.

2- Modularize the application

Modern C++ librarires use extensively the namespaces to modalirize their code base, and they use the  “Namespace-by-feature” approach. Namespace-by-feature uses namespaces to reflect the feature set. It places all items related to a single feature (and only that feature) into a single namespace. This results in namespaces with high cohesion and high modularity, and with minimal coupling between namespaces. Items that work closely together are placed next to each other.

Boost is the best example of grouping by feature, it contains thousands of namespaces, each one is used to group a specific feature.

3- Anonymous namespace.

Namespace with no name avoids making global static variable. The “anonymous” namespace you have created will only be accessible within the file you created it in.

4- workarround of the enum issue.

“Traditional” enums in C++ export their enumerators in the surrounding scope ,which can lead to name collisions, if two different enums in the same have scope define enumerators with the same name,

In a large project, you would not be guaranteed that two distinct enums don’t both called with the same name. This issue was resolved in C++11, using  enum class which will implicitly scope the enum values within the enum’s name.

Many years ago the trick of declaring an enum inside a namespace is used, for example instead of declaring an enum like this

enum status{

it’s declared inside an namespace:

namespace status{
       enum status{

Many C++ projects use this trick, for example the Unreal Engine source code use widely this technique.

5- Hiding details by convention

For templated libraries where the code is implemented in header files, it’s interesting to find a way to inform the library user that he dont need to use directly some specific types because they concern only the implementation. In C# the “internal” keyword did the job, but in C++ there’s no way to hide public types to the library user.

A common idiom in modern C++, pioneered by the developers of the Boost libraries, is to separate symbols that form part of the implementation of your module (that is, don’t form part of the public API) but that have to be publicly available into a separate sub-namespace, by convention named detail.

For example in the boost::math documentation, it’s specified that:

Functions not intended for use by applications are in boost::math::detail.