Home C++ Programming Tutorials Namespace in C++

Namespace in C++

by adarshpal

In this article, you’ll learn about Namespace in C++, Declaring namespaces, Accessing namespace using “using” directive , The global namespace, The std namespace, Nested namespaces and more.

What is Namespace in C++ ?

Namespace are used to provide scope for the identifiers like functions, variables, classes etc, with the same name available in different libraries.All identifiers inside a same namespace are visible to one another.The identifiers outside the namespace scope can be accessed by using fully qualified name for each identifier.

For example

std::vector<std::string> vec;

The variables declared in 2 different namespace can have same name.

Declaring namespaces 

namespace MyNamespaceName
{
//code declarations
}

The identifiers can be accessed by scope resolution operator.

Example

MyNamespaceName::code;

Full Code Example

#include <iostream>  
using namespace std;  
// First namespace

namespace FirstNameSpace 

{
   void func() 
   {
      cout << "Inside FirstNameSpace" << endl;
   }
}
// Second namespace
namespace SecondNameSpace 
{
   void func() 
   {
      cout << "Inside SecondNameSpace" << endl;
   }
}
int main () 
{
   // Calling first namespace

   FirstNameSpace::func();

   // Calling second namespace

   SecondNameSpace::func(); 

   return 0;
}

Output:

Inside FirstNameSpace
Inside SecondNameSpace

Accessing namespace using “using” directive

You can access the namespace by using “using” directive.

#include <iostream>  
using namespace std;  
// First namespace
namespace FirstNameSpace 
{
   void func() 
   {
      cout << "Inside FirstNameSpace" << endl;
   }
}
using namespace FirstNameSpace;
int main () 
{
   func();
   return 0;
}

The global namespace

Identifiers defined outside of all the namespaces will be called as global namespaces. It is recommended not to create a global namespace. An exception is for the entry point for main function, that requires global namespace to work.To access an global namespace identifier use  scope resolution operator “::”.

For example:

::myGlobalFunction();

This will differentiate between the global identifier and any other local identifier with the same name.

The std namespace

All of the standard C++ libraries are defined in “std” namespace or nested inside “std” namespace.

Nested namespaces

Namespace can be declared inside another namespace. Then it is called as Nested namespace.

Example:

#include <iostream>  
using namespace std; 
namespace OutsideNamespace 
{
    int a;
    int b;
    // Second namespace

    namespace InsideNamespace 
    {
       void add(int num1, int num2) 
       {
          cout << "Sum is "<<(num1 + num2) << endl;
       }
   }
}
int main () 
{
   OutsideNamespace::a = 10;
   OutsideNamespace::b = 20;
   OutsideNamespace::InsideNamespace::add(OutsideNamespace::a, OutsideNamespace::b);
   return 0;

}

Output:

Sum is 30

Inline namespaces (C++ 11)

Inline namespaces are introduced in C++ 11.Inline namespaces are used as a nested namespace.They are treated as a normal namespace instead of treating as a nested namespace.

Example:

#include <iostream>  
using namespace std;  
namespace Foo 
{
    // Inline namespace
    inline namespace Bar 
    {
       void barFun() 
       {
          cout << "In bar Func"<< endl;
       }
    }
}
int main () 
{

    //barFun can be called with the parent namespace
    Foo::barFun();
   return 0;
}

Output:

In bar Func

But what is the use of inline namespace?

It is useful in version control. Suppose you released new updated version of a function and it is not backward compatible. At that time you can use inline namespace.

It can be shown as below:

#include <iostream>  
using namespace std;  
namespace Foo 
{
    namespace v1
    {
       void fooFun() 
       {

          cout << "In old foo Func"<< endl;
       }        
    }
    // Inline namespace
    inline namespace v2 
{
       void fooFun() 
       {
          cout << "In new foo Func"<< endl;
       }  
    }
}
int main () 
{
    //now if you cann fooFun(), latest v2 fooFun will be called.
    Foo::fooFun();
    //now if the user wants the older function to be called, then he can call as below:
    Foo::v1::fooFun();
   return 0;
}

Output:

In new foo Func

In old foo Func  

Namespace aliases

You can give another name for a namespace.

Example:

#include <iostream>  
using namespace std;  
namespace this_namespace_name_is_long 
{
    void foo()
    {
        cout<<"In foo function"<<endl;
}
}
namespace stortName = this_namespace_name_is_long;
int main () 
{
    stortName::foo();
   return 0;
}

Output:

In foo function

Anonymous or unnamed namespaces

Creating a namespace without any name is called as anonymous or unnamed namespaces.It is used to create internal linkage for functions and classes declared inside the namesapce. They can be accessed inside the file that they are declared.The same functions and classes cannot be accessed from other file.

Example:

#include <iostream>  
using namespace std;  
namespace 
{
    void foo()
    {
        cout<<"In foo function"<<endl;
}
}
int main () 
{
    foo();
   return 0;
}

Output:

In foo function

Discontiguous Namespace in C++

C++ allows you to define a same namespace spread in various files.Then during the compilation step it will be combined.

Example:

namespace namespace_name 
{
   // declarations
}

You may also like

Adblock Detected

Please support us by disabling your AdBlocker extension from your browsers for our website.