C++ namespace 5 common usage instance parsing

  • 2020-10-07 18:48:10
  • OfStack

For those of you who are familiar with C++ but are not familiar with the common use of namespaces, this article describes the first section of C++.

Namespaces were introduced to the c++ standard in 1995 and are generally defined as follows:

A namespace defines a new scope. They provide a way to avoid name conflicts. Namespaces in c++ are often used to avoid naming conflicts. Although namespaces are widely used in recent c++ code, most older code does not use this tool.

Based on the exploration and research of the source code of many C++ projects, some common reasons for using namespaces in these projects are summarized.

1- Avoid name conflicts

As mentioned earlier, this is the most common reason for using namespaces in C++, in which case their use is only valid for the compiler. It doesn't add much value to the developer in terms of code readability and maintainability.

2- Modular applications

The namespace takes the "ES23en-ES24en-ES25en" approach to modularizing code. Namespace-by-feature "reflects the feature set through namespaces. It groups items that have interdependencies into the same namespace. This results in high cohesion and low coupling. Coupling is a measure of the degree of interconnection between different modules within a software structure.

Boost is the best example of grouping by function, containing thousands of namespaces, each one used to group specific functions.

3- Anonymous namespace

Anonymous namespaces prevent the generation of global static variables. The anonymous namespace you create can only be accessed from the file in which it was created.

4- Resolved enumeration type member name problem

If an enumeration with the same name is defined within the same scope, the "traditional" enumeration in C++ will export the enumeration values within its scope, which may cause name conflicts,

In a large project, there is no guarantee that two different enumerations will not have the same name. This problem has been solved in C++ 11, which USES enumeration classes to implicitly define enumeration values in enumeration names.

Many years ago, this problem was solved using the trick of declaring enumerations within namespaces, rather than declaring enumerations like this


enum status{ 
  status_ok, 
  status_error 
};
enum status{ 
  status_ok, 
  status_error 
};

It declares in the namespace:


namespace status{ 
  enum status{ 
   ok,
   error 
 }; 
}
namespace status{ 
  enum status{ 
   ok,
   error 
 }; 
}

Many c++ projects use this technique, such as the Unreal Engine source code, which is widely used.

5- Hide the implementation

For template libraries implemented in header files, developers do not need to use special data types when invoking them because they are only focused on the implementation of the functionality, so it is interesting to find a way for developers to invoke the library. In c#, the "internal" keyword does this, but in c++, there is no way to completely hide public data types from the developer.

Separating the definition and implementation in the module is an c++ idiom created by the Boost library developers, but these definitions must be placed in a subnamespace where the developer can call ES71en-ES72en (subnamespace).


Related articles: