C++ Namespaces
Suppose a situation where there are two students named Zara in a class; to clearly distinguish them, besides using their names, we have to use some extra information, such as their home addresses or their parents' names.
The same situation also occurs in C++ applications. For example, you might write a function named xyz(), and there is also an identical function xyz() in another available library. In this way, the compiler cannot determine which xyz() function you are using.
Therefore, the concept ofNamespacingwas introduced, specifically to solve the above problem; it serves 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 give an example in a computer system: a folder (directory) can contain multiple subfolders; each subfolder cannot have the same file name, but files in different subfolders can have the same name.

Defining a namespace
A namespace definition uses the keyword namespace, followed by the namespace name, as shown below:
namespace
namespace_name
{
// Code declaration
}
To call a function or variable with a namespace, you need to prefix it with the namespace name, as shown below:
name
::
code
;
// code can be a variable or function
Let's see 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 code above is compiled and executed, it produces the following result:
Inside first_space
Inside second_space
using directive
You can use the using namespace directive, so that when using a namespace you don't have to prefix it with the namespace name. This directive tells the compiler that 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 code above is compiled and executed, it produces the following result:
Inside first_space
The using directive can also be used to specify a particular item in a namespace. For example, if you only intend to use the cout part of the std namespace, you can use the following statement:
using std::cout;
In subsequent code, you can use cout without prefixing it with the namespace name, but std other 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 code above is compiled and executed, it produces the following result:
std::endl is used with std!
using the names introduced by the directive follow normal scope rules. The name is introduced from the use of using The directive is visible from the point of its use until the end of the scope. At this point, entities with the same name defined outside the scope are hidden.
Discontiguous namespaces
A namespace can be defined in several separate parts, so a namespace is composed of several individually defined parts. The various components of a namespace can be spread across multiple files.
Therefore, if a component of a namespace needs to refer to a name defined in another file, that name still needs to be declared. The following namespace definition can either 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 access members of a nested namespace using the :: operator:
// Access members in namespace_name2
using
namespace
namespace_name1
::
namespace_name2
;
// Access members in namespace_name1
using
namespace
namespace_name1
;
In the statement above, if namespace_name1 is used, then the elements of 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 code above is compiled and executed, it produces the following result:
Inside second_space