A namespace alias in C++ is a declarative mechanism that establishes an alternative identifier (a synonym) for an existing, previously defined namespace. It binds a new name to a specific namespace scope, allowing the compiler to treat the alias exactly as the original namespace identifier during qualified name lookup, without creating a new declarative region or altering the original namespace.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.
Syntax
The declaration utilizes thenamespace keyword followed by the new alias identifier, an equals sign (=) acting as a punctuator, and the name of the target namespace (which may be unqualified, qualified, or fully qualified).
Mechanics and Resolution
When a namespace alias is declared, it does not copy or duplicate the entities within the target namespace. Instead, it acts as a direct reference to the original namespace’s declarative region. Any additions made to the original namespace after the alias is declared are immediately accessible through the alias.Scoping Rules
Namespace aliases obey standard C++ lexical scoping rules. The visibility of the alias depends entirely on the declarative region where it is introduced:- Global Scope: If declared outside of any namespace or block, the alias is in the global namespace scope and is visible from the point of declaration to the end of the translation unit.
- Namespace Scope: If declared within a named namespace, the alias becomes a member of that enclosing namespace. Its unqualified visibility is restricted to that namespace, though it can be accessed externally using qualified name lookup.
- Block Scope: If declared within a block (such as a function body), the alias is only visible within that specific block.
- Class Scope Restriction: Namespace aliases are strictly forbidden at class scope. They are not valid
member-declarations and cannot be declared inside aclass,struct, orunion.
Aliasing an Alias
C++ permits the creation of a namespace alias that targets another existing namespace alias. The compiler resolves the chain of aliases back to the original, underlying namespace during compilation.Master C++ with Deep Grasping Methodology!Learn More





