Namespace aliases
From cppreference.com
C++
Feature test macros (C++20)
Concepts library (C++20)
Metaprogramming library (C++11)
Ranges library (C++20)
Filesystem library (C++17)
Concurrency support library (C++11)
Execution control library (C++26)
C++ language
General topics
Conditional execution statements
Iteration statements (loops)
Jump statements
Dynamic exception specifications (until C++17*)
noexcept
specifier (C++11) Exceptions
Namespaces
Namespace aliases
Types
Specifiers
User-defined (C++11)
Utilities
Attributes (C++11)
Types
Type alias declaration (C++11)
Casts
Memory allocation
Class-specific function properties
Special member functions
Miscellaneous
Declarations
Overview
Specifiers
(C++11)
(C++20)
(C++20)
Translation-unit-local (C++20)
(C++11)
(C++11)
(C++11)
Pack indexing specifier (C++26)
Attributes (C++11)
Declarators
Block declarations
→Structured binding declaration (C++17)
Alias declaration (C++11)
Namespace alias definition
static_assert declaration (C++11)
Opaque enum declaration (C++11)
Other declarations
Explicit template instantiation (C++11)
Attribute declaration (C++11)
Namespace aliases allow the programmer to define an alternate name for a namespace.
They are commonly used as a convenient shortcut for long or deeply-nested namespaces.
Contents
[edit] Syntax
namespace
alias_name = ns_name;
(1)
namespace
alias_name = ::
ns_name;
(2)
namespace
alias_name = nested_name::
ns_name;
(3)
[edit] Explanation
The new alias alias_name provides an alternate method of accessing ns_name.
alias_name must be a name not previously used. alias_name is valid for the duration of the scope in which it is introduced.
[edit] Keywords
[edit] Example
Run this code
#include <iostream> namespace foo { namespace bar { namespace baz { int qux = 42; } } } namespace fbz = foo::bar::baz; int main() { std::cout << fbz::qux << '\n'; }
Output:
42
[edit] See also
namespace declaration
identifies a namespace[edit]