Namespaces
Variants

Namespace aliases

From cppreference.net
C++ language
General topics
Flow control
Conditional execution statements
Iteration statements (loops)
Jump statements
Functions
Function declaration
Lambda function expression
inline specifier
Dynamic exception specifications ( until C++17* )
noexcept specifier (C++11)
Exceptions
Namespaces
Namespace aliases
Types
Specifiers
constexpr (C++11)
consteval (C++20)
constinit (C++20)
Storage duration specifiers
Initialization
Expressions
Alternative representations
Literals
Boolean - Integer - Floating-point
Character - String - nullptr (C++11)
User-defined (C++11)
Utilities
Attributes (C++11)
Types
typedef declaration
Type alias declaration (C++11)
Casts
Memory allocation
Classes
Class-specific function properties
Special member functions
Templates
Miscellaneous

命名空间别名允许程序员为命名空间定义一个替代名称。

它们通常用作长命名空间或深层嵌套命名空间的便捷快捷方式。

目录

语法

namespace alias_name = ns_name ; (1)
namespace alias_name = :: ns_name ; (2)
namespace alias_name = nested_name :: ns_name ; (3)

说明

新的别名 alias_name 提供了访问 ns_name 的另一种方法。

alias_name 必须是先前未使用过的名称。 alias_name 在其声明的作用域持续期间保持有效。

关键词

namespace

示例

#include <iostream>
namespace foo
{
    namespace bar
    {
         namespace baz
         {
             int qux = 42;
         }
    }
}
namespace fbz = foo::bar::baz;
int main()
{
    std::cout << fbz::qux << '\n';
}

输出:

42

另请参阅

namespace声明 标识一个命名空间
type alias声明 (C++11) 为类型创建同义词