> ## Documentation Index
> Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
> Use this file to discover all available pages before exploring further.

# C++ Nested Namespace

A nested namespace in C++ is a namespace declared entirely within the declarative region of another namespace. It establishes a hierarchical scope resolution system. Members defined in deeper lexical scopes can be accessed from outside using explicit qualification via the scope resolution operator (`::`), or through `using` declarations and `using` directives to bypass explicit qualification.

## Traditional Syntax (Pre-C++17)

Historically, defining a nested namespace required explicitly nesting the lexical blocks. Each namespace introduces its own scope, and inner scopes have implicit access to enclosing scopes via unqualified name lookup.

```cpp theme={"dark"}
namespace Outer {
    int outer_var = 10;

    namespace Inner {
        int inner_var = 20;
        
        int calculate() {
            // Unqualified name lookup resolves outer_var
            return outer_var + inner_var; 
        }
    }
}
```

## Nested Namespace Definition (C++17)

C++17 introduced a concise syntax for nested namespace definitions. This allows multiple namespace levels to be declared in a single declarative step by concatenating the namespace identifiers with the scope resolution operator.

```cpp theme={"dark"}
namespace Outer {
    int existing_var = 5;
}

namespace Outer::Inner {
    int inner_var = 20;
    
    int get_sum() {
        return existing_var + inner_var;
    }
}
```

*Note: This syntax is strictly equivalent to traditional nested blocks. While it does not reopen the `Outer` namespace to add new declarations, code inside `Outer::Inner` retains access to previously declared members of `Outer` via standard unqualified name lookup.*

## Scope Resolution and Access

To access members of a nested namespace from outside its enclosing scopes, identifiers must be fully qualified starting from the global scope or the nearest accessible enclosing scope.

```cpp theme={"dark"}
#include <iostream>

namespace Outer {
    namespace Inner {
        int inner_var = 42;
        void display() {
            std::cout << inner_var << '\n';
        }
    }
}

int main() {
    // Fully qualified name
    Outer::Inner::display();
    
    // Accessing variables
    int val = Outer::Inner::inner_var;
    
    return 0;
}
```

## Using Declarations and Directives

To mitigate the verbosity of deep qualification, C++ provides `using` declarations and `using` directives. A `using` declaration introduces a specific nested namespace member into the current scope, while a `using` directive makes all names from the nested namespace available for unqualified lookup.

```cpp theme={"dark"}
namespace Outer {
    namespace Inner {
        int value = 100;
        void process() {}
    }
}

void example_declaration() {
    using Outer::Inner::value; // using declaration
    value = 200;               // Resolves to Outer::Inner::value
}

void example_directive() {
    using namespace Outer::Inner; // using directive
    process();                    // Resolves to Outer::Inner::process
}
```

## Namespace Aliasing

Namespace aliasing creates a local synonym for a nested namespace within the current translation unit or block scope, providing another mechanism to shorten long namespace chains.

```cpp theme={"dark"}
namespace System {
    namespace Network {
        namespace Protocols {
            namespace TCP {
                struct Socket {
                    int id;
                };
            }
        }
    }
}

// Create an alias for the deeply nested namespace
namespace CoreNet = System::Network::Protocols::TCP;

int main() {
    CoreNet::Socket socket; // Resolves to System::Network::Protocols::TCP::Socket
    socket.id = 1;
    return 0;
}
```

## Inline Nested Namespaces (C++20)

C++20 extended the C++17 syntax to support the `inline` specifier within a concatenated nested namespace definition. Members of an inline nested namespace are automatically injected into the immediate enclosing namespace's scope.

```cpp theme={"dark"}
#include <iostream>

// C++20 syntax
namespace Outer::inline Inner {
    void function() {
        std::cout << "Inline nested namespace function\n";
    }
}

// Equivalent to:
// namespace Outer {
//     inline namespace Inner {
//         void function() { ... }
//     }
// }

int main() {
    Outer::function(); // Accessible directly through the enclosing namespace
    return 0;
}
```

<div
  style={{ 
display: "flex", 
justifyContent: "space-between", 
alignItems: "center", 
maxWidth: "754px", 
padding: "1rem 0",
marginBottom: "24px"
}}
>
  <span style={{ fontWeight: "bold", fontSize: "1.25rem", color: "var(--tw-prose-headings)", fontFamily: "Inter, ui-sans-serif, system-ui, sans-serif" }}>Tired of Poor C++ Skills? Fix That With Deep Grasping!</span>

  <a
    href="https://syntblaze.com"
    target="_blank"
    style={{ 
  marginLeft: "24px",
  textDecoration: "none", 
  backgroundColor: "#007AFF",
  color: "#ffffff", 
  padding: "6px 16px", 
  borderRadius: "16px",
  fontSize: "0.9rem",
  fontWeight: "600",
  textAlign: "center",
  transition: "background-color 0.2s ease"
}}
  >
    Learn More
  </a>
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/skill-tracking.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=b9b0305c93bb501c9e767b5c76c88835" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/skill-tracking.png" />

  <img src="https://mintcdn.com/syntblazellc/23tyuOzaWS88qFlc/images/nuggets.png?fit=max&auto=format&n=23tyuOzaWS88qFlc&q=85&s=c86c80197299762989e9b882419b2109" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/nuggets.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/bite-sized-exercises.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=a65f9a38c37ff28ab73ed783c53c60e3" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/bite-sized-exercises.png" />
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap", marginTop: "12px" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/mastery-chain.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=748a1763454713e679260fbb95f154a2" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/mastery-chain.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-previews.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=242f61448ff5dd6deaaab2dccc13b507" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-previews.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-explanations.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=cf0fc1c31f9cd0fc26716781be05fbc9" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-explanations.png" />
</div>
