Nested Inline Namespaces In C++20 Last Updated : 11 Nov, 2023 Comments Improve Suggest changes Like Article Like Report The Inline Namespace is a feature that was first introduced in C++ 11. They can also be nested inside another namespace and the members of this nested namespace can be accessed as if they are the members of the parent namespace. Their syntax may seem complex so in C++20, a new version introduced a new syntax for declaring/defining Nested Inline Namespaces which makes them more flexible and expressive. Prerequisite: C++ Inline Namespaces, C++ Namespace Nesting. Traditional Syntax (C++11 to C++17) In earlier versions, the inline namespaces were defined as: namespace my_namespace { inline namespace nested_namespace { // Members of nested_namespace } }C++20 Nested Inline Namespaces Syntax In C++ 20, we can define the Nested Inline Namespaces like: namespace my_namespace :: inline nested_namespace { // Members of nested_namespace } The new syntax is much easier to manage and implement. Example The following example demonstrates the use of the new syntax of nested inline namespaces in C++: C++ // C++ Program to illustrate the use of new nested inline // namespace syntax #include <iostream> namespace old_parent_ns { inline old_nested_ns1 { namespace old_nested_ns2 { void func() { std::cout << "Function from Old Defintion\n"; } } } } // declaring same namespace using new definition syntax namespace new_parent_ns ::inline new_nested_ns1 :: new_nested_ns2 { void func() { std::cout << "Function from New Definition"; } } // driver code int main() { old_parent::old_nested_ns2::func(); new_parent::new_nested_ns2::func(); return 0; } Output Function from Old Defintion Function from New Definition As we can see, both of the definition works the same. It depends upon our requirement which one we want to use. Comment More infoAdvertise with us Next Article Nested Inline Namespaces In C++20 M maha123 Follow Improve Article Tags : C++ Geeks Premier League Geeks Premier League 2023 Practice Tags : CPP Similar Reads Can namespaces be nested in C++? In C++, namespaces can be nested, and resolution of namespace variables is hierarchical. For example, in the following code, namespace inner is created inside namespace outer, which is inside the global namespace. In the line "int z = x", x refers to outer::x. If x would not have been in outer then 2 min read Namespace in C++ Name conflicts in C++ happen when different parts of a program use the same name for variables, functions, or classes, causing confusion for the compiler. To avoid this, C++ introduce namespace.Namespace is a feature that provides a way to group related identifiers such as variables, functions, and 6 min read Inline Variables in C++ 17 An inline variable in C++ is a variable that is declared using an inline specifier. It is an exception to one definition having multiple definitions across various translation units. Inline variables have an external linkage when not declared as static. Syntaxinline data_type variable_name = initial 3 min read namespace in C++ | Set 2 (Extending namespace and Unnamed namespace) We have introduced namespaces in below set 1.Namespace in C++ | Set 1 (Introduction) Defining a Namespace: A namespace definition begins with the keyword namespace followed by the namespace name as follows: namespace namespace_name {// code declarations i.e. variable (int a;)method (void add();)clas 4 min read Cons of using the whole namespace in C++ A namespace is a declarative region that provides a scope to the identifiers (the names of types, functions, variables, etc) inside it. Namespaces are used to organise code into logical groups and to prevent name collisions that can occur especially when your code base includes multiple libraries. P 2 min read Like