Prerequisite : Namespaces in C++
An inline namespace is a namespace that uses the optional keyword inline in its original-namespace-definition.
We can see from 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: 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.
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:
- Avoid verbose :Consider the above code, if you want to print “var”, you write:
cout << ns1::ns2::ns3::var;
This looks good only if 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.
- Support of Library :The inline namespace mechanism is intended to support library evolution by providing a mechanism that supports a form of versioning. Refer this for details.
This same behavior (same as inline namesapces) 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). Consider the following C++ code:
Here again, the using directive makes the declarations from the nested namespace appear exactly as if they had been declared in the enclosing namespace.
Also see: Nesting of namespaces
This article is contributed by Arnav Shrivastava . If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don't stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Namespaces in C++ | Set 4 (Overloading, and Exchange of Data in different Namespaces)
- Can namespaces be nested in C++?
- Difference between Inline and Macro in C++
- Difference between virtual function and inline function in C++
- Inline Functions in C++
- Inline function in C
- #pragma Directive in C/C++
- C/C++ #include directive with Examples
- How to create a dynamic 2D array inside a class in C++ ?
- Nested printf (printf inside printf) in C
- What happens when a virtual function is called inside a non-virtual function in C++
- Where is an object stored if it is created inside a block in C++?
- Pointers in C and C++ | Set 1 (Introduction, Arithmetic and Array)
- What are the differences between bitwise and logical AND operators in C/C++?
- Difference and Similarities between PHP and C
- INT_MAX and INT_MIN in C/C++ and Applications
- fesetround() and fegetround() in C++ and their application
- Lex program to take input from file and remove multiple spaces, lines and tabs
- Queries to insert, delete one occurrence of a number and print the least and most frequent element
- Get first and last elements from Array and Vector in CPP