Open In App
Related Articles

C++ Inline Namespaces and Usage of the “using” Directive Inside Namespaces

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

Prerequisite: Namespaces in C++

In C++, namespaces can be nested, and the resolution of namespace variables is hierarchical. An inline namespace is a namespace that uses the optional keyword inline in its original-namespace definition. This allows the identifiers of the nested inline namespace to behave as if they are the identifier of the parent/enclosing namespace.

Syntax:

inline namespace namespace_name;

Example:

C++

// C++ program to demonstrate working of
// inline namespaces
#include <iostream>
using namespace std;
  
namespace ns1 {
inline namespace ns2 {
    int var = 10;
}
} // namespace ns1
  
int main()
{
    cout << ns1::var;
    return 0;
}

                    

Output
10

We can see in the above example that members of an inline namespace are treated as if they are members of the enclosing namespace in many situations (listed below). This property is transitive i.e if a namespace N contains an inline namespace M, which in turn contains an inline namespace O, then the members of O can be used as though they were members of M or N.

Example:

C++

// C++ program to demonstrate working of
// inline namespaces inside inline namespaces
  
#include <iostream>
using namespace std;
  
namespace ns1 {
inline namespace ns2 {
    inline namespace ns3 {
        int var = 10;
    }
} // namespace ns2
} // namespace ns1
  
int main()
{
    cout << ns1::var;
    return 0;
}

                    

Output
10


The inline specifier makes the declarations from the nested namespace appear exactly as if they had been declared in the enclosing namespace. This means it drags out the declaration (“var” in the above example) from a nested namespace to the containing namespace.

Advantages of using inline namespaces

1. Avoid verbose: Consider the above code, if you want to print “var”, you write: 

cout << ns1::ns2::ns3::var;

2. This looks good only if the namespace’s names are short as in the above example. But by using inline with namespaces there is no need to type the entire namespace as given above or use the “using” directive.

3. Support of Library: The inline namespace mechanism is intended to support library evolution by providing a mechanism that supports a form of versioning.  

“Using” directive

This same behavior (same as inline namespaces) can also be achieved by using the “using” declarative inside namespaces. A using-directive that names the inline namespace is implicitly inserted in the enclosing namespace (similar to the implicit using-directive for the unnamed namespace).

Syntax:

using namespace namespace_name;

Example:

C++

// C++ program to demonstrate working
// of "using" to get the same effect as
// inline.
#include <iostream>
using namespace std;
  
namespace ns1 {
namespace ns2 {
    namespace ns3 {
        int var = 10;
    }
    using namespace ns3;
} // namespace ns2
  
using namespace ns2;
} // namespace ns1
  
int main()
{
    cout << ns1::var;
    return 0;
}

                    

Output
10

Here again, the using directive makes the declarations from the nested namespace appear exactly as if they had been declared in the enclosing namespace.



Last Updated : 09 Mar, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads