Namespace in C++

Namespace is a keyword which is used to define a scope for identifiers like variables, functions, classes, etc with the same names. Namespace was introduced in C++ as they were not presented in C language. A namespaces are typically used to prevent conflicts between identifiers with the same name.

Creation of Namespaces

Creation of namespace is similar to creating a class, but the only difference is that it doesn't end with a semicolon like a class does. We use a namespace keyword followed by the name of the namespace.

Syntax

namespace namespace_name{
    // Function, class, and variable declarations.
}

Features of Namespace

  • Declaration of namespace only appears at global scope.
  • The definition of namespaces can be used over multiple files.
  • Namespace declarations do not have public, private or protected specifiers because they are declared in global scopes and can be easily nested in other namespaces.
  • Namespace definition does not require a semicolon at the end.

The using directive in C++

The using keyword indicates the compiler that the variables, classes and functions declared inside a namespace are being used in the program. By doing so, we don't need to attach the name of the namespace before the members of that namespace.

Syntax for the using directive

using namespace namespace_name;
Example for the using directive

#include <iostream>
using namespace std;
namespace first
{
  int x = 5;
  int y = 10;
}
namespace second
{
  double x = 3.16;
  double y = 2.73;
}
int main () {
  using first::x;
  using second::y;
  cout << x << endl;
  cout << y << endl;
  cout << first::y << endl;
  cout << second::x << endl;
  return 0;
}
Output
5
2.73
10
3.16

Nested Namespaces in C++

Namespace can be nested. It means that we can define one namespace inside another namespace.

Syntax for Nested Namespace

namespace namespace_name1 {
  // code declarations
  namespace namespace_name2 {
     // code declarations
  }
}

You can access members of nested namespace by using resolution operators :


// to access members of namespace_name2
using namespace namespace_name1::namespace_name2;

// to access members of namespace:name1
using namespace namespace_name1;
Example for Nested Namespace

#include <iostream>
using namespace std;

// first name space
namespace first_space
{
  void func()
  {
    cout << "Inside first_space" << endl;
  }
  // second name space
  namespace second_space
  {
    void func()
    {
      cout << "Inside second_space" << endl;
    }
  }
}

using namespace first_space::second_space;

int main()
{

  // This calls the function from the second name space.
  func();

  return 0;
}
Output
Inside second_space

How namespace works in C++

When a namespace is declared using the namespace keyword, all the variables, functions, or classes declared inside it can be accessed using the namespace name and the scope resolution operator ::. When the compiler meets the scope resolution operator, it sends the control to the namespace block.

Unnamed Namespace

When a namespace is unnamed, the members declared inside the namespace block can be explicitly accessed without using the scope resolution operator as shown in the example below:

Example for Nested Namespace

#include <iostream>
using namespace std;
 
// unnamed namespace declaration
namespace
{
   int num= 100;
}
 
int main()
{
   cout << num; // prints 100
   return 0;
}
Output
100