C++ Namespace
Suppose there is a situation where there are two students named Zara in a class. To distinguish between them, we need to use additional information beyond their names, such as their home addresses or their parents' names.
A similar situation occurs in C++ applications. For example, you might write a function named xyz()
, and there might be another identical function xyz()
in an available library. In this case, the compiler cannot determine which xyz()
function you are using.
To address this issue, the concept of namespace was introduced. It serves as additional information to differentiate functions, classes, variables, etc., with the same names in different libraries. Using a namespace defines a context. Essentially, a namespace defines a scope.
Let's consider an example from a computer system: a folder (directory) can contain multiple subfolders, and each subfolder cannot have the same filename, but files in different subfolders can have the same name.
Defining a Namespace
A namespace is defined using the keyword namespace, followed by the namespace name, as shown below:
namespace namespace_name {
// code declarations
}
To call the function or variable with the namespace, you need to prepend the namespace name, as follows:
name::code; // code can be a variable or function
Let's see how a namespace defines the scope for entities like 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 function from the first namespace
first_space::func();
// Call function from 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 use the using namespace directive, which allows you to use the namespace without prefixing 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 function from 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 particular items within a namespace. For example, if you intend to use only the cout
part from the std
namespace, you can use the following statement:
using std::cout;
In subsequent code, you can use cout
without the namespace prefix, but other items from the std
namespace still require the namespace prefix, 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!
The names introduced by the using directive follow normal scope rules. The names become visible from the point of the using directive to the end of the scope in which the directive is found. At this point, the identically named entities defined outside the scope are hidden.
Discontinuous Namespaces
A namespace can be defined in several distinct parts, so a namespace is made up of the sum of its separately defined parts. The components of a namespace can be spread over multiple files. Therefore, if a component within a namespace needs to request a name defined in another file, it still needs to declare that name. The following namespace definition can either define a new namespace or add new elements to an existing namespace:
namespace namespace_name {
// Code declarations
}
Nested Namespaces
Namespaces can be nested; you can define one namespace inside another, as shown below:
namespace namespace_name1 {
// Code declarations
namespace namespace_name2 {
// Code declarations
}
}
You can access members within nested namespaces 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 above statements, if namespace_name1 is used, then elements within namespace_name2 are also available within that scope, 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