C++ namespace

Suppose there is a situation where there are two students named Zara in a class. To clearly distinguish between them, besides using their names, we have to use some additional information, such as their home addresses, or their parents' names, and so on.

The same situation also occurs in C++ applications. For example, you might write a function named xyz(), and the same function xyz() also exists in another available library. In this way, the compiler cannot determine which xyz() function you are using.

Therefore, it introducesnamespaceThis concept is specifically used to solve the above problem. It can serve as additional information to distinguish functions, classes, variables, etc. with the same name in different libraries. Using a namespace defines a context. Essentially, a namespace defines a scope.

Let's take an example from a computer system: a folder (directory) can contain multiple folders. Within each folder, file names cannot be the same, but files in different folders can share the same name.

Define a namespace

The definition of a namespace uses the keywordnamespace, followed by the name of the namespace, as shown below:

namespace namespace_name { // Code declaration }

To call a function or variable with a namespace, you need to prepend the namespace name before it, as shown below:

name::code; // code can be a variable or function

Let's take a look at how a namespace defines a scope for entities such as variables or functions:

Example

#include <iostream> using namespace std; // first namespace namespace first_space{ void func(){ cout << "Inside first_space" << endl; } } // second namespace namespace second_space{ void func(){ cout << "Inside second_space" << endl; } } int main () { // call the function in the first namespace first_space::func(); // call the function in the second namespace second_space::func(); return 0; }

When the above code is compiled and executed, it produces the following result:

Inside first_space
Inside second_space

using directive

You can useusing namespacedirective, so that when using the namespace, you don't need to prepend the namespace name. This directive tells the compiler that the subsequent code will use names from the specified namespace.

Example

#include <iostream> using namespace std; // first namespace namespace first_space{ void func(){ cout << "Inside first_space" << endl; } } // second namespace namespace second_space{ void func(){ cout << "Inside second_space" << endl; } } using namespace first_space; int main () { // call the function in the first namespace func(); return 0; }

When the above code is compiled and executed, it produces the following result:

Inside first_space

The using directive can also be used to specify specific items in a namespace. For example, if you only intend to use the cout part of the std namespace, you can use a statement like the following:

using std::cout;

In the subsequent code, when using cout, you don't need to prefix it with the namespace name, butstdother items in the namespace still need to be prefixed with the namespace name, as shown below:

Example

#include <iostream> using std::cout; int main () { cout << "std::endl is used with std!" << std::endl; return 0; }

When the above code is compiled and executed, it produces the following result:

std::endl is used with std!

usingThe names introduced by the directive follow the normal scope rules. The names are visible from the use of theusingdirective until the end of that scope. At this point, entities with the same name defined outside the scope are hidden.

Discontiguous namespaces

A namespace can be defined in several different parts, so a namespace is composed of several separately defined parts. The various components of a namespace can be scattered across multiple files.

Therefore, if a component of a namespace needs to request a name defined in another file, it still needs to declare that name. The following namespace definition can define a new namespace or add new elements to an existing namespace:

namespace namespace_name { // Code declaration }

Nested namespaces

Namespaces can be nested. You can define another namespace within a namespace, as shown below:

namespace namespace_name1 { // Code declaration namespace namespace_name2 { // Code declaration } }

You can use::operator to access members in a nested namespace:

// access members in namespace_name2 using namespace namespace_name1::namespace_name2; // access members in namespace_name1 using namespace namespace_name1;

In the above statement, if namespace_name1 is used, then elements in namespace_name2 within that scope are also available, as shown below:

Example

#include <iostream> using namespace std; // first namespace namespace first_space{ void func(){ cout << "Inside first_space" << endl; } // second namespace namespace second_space{ void func(){ cout << "Inside second_space" << endl; } } } using namespace first_space::second_space; int main () { // call the function in the second namespace func(); return 0; }

When the above code is compiled and executed, it produces the following result:

Inside second_space
other extensions