Namespaces
Variants
Views
Actions

Namespace aliases

From cppreference.com
< cpp‎ | language
Revision as of 08:13, 3 January 2013 by Ggia (Talk | contribs)

 
 
C++ language
General topics
Flow control
Conditional execution statements
Iteration statements (loops)
Jump statements
Functions
Function declaration
Lambda function declaration
inline specifier
Exception specifications (deprecated)
noexcept specifier (C++11)
Exceptions
Namespaces
Namespace aliases
Types
Specifiers
decltype (C++11)
auto (C++11)
alignas (C++11)
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
Implicit conversions - Explicit conversions
static_cast - dynamic_cast
const_cast - reinterpret_cast
Memory allocation
Classes
Class-specific function properties
Special member functions
Templates
Miscellaneous
 

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

Syntax

namespace Template:sparam = Template:sparam; (1)
namespace Template:sparam = ::Template:sparam; (2)
namespace Template:sparam = Template:sparam::Template:sparam; (3)

Explanation

The new alias Template:sparam provides an alternate method of accessing Template:sparam.

Template:sparam must be a name not previously used. Template:sparam is valid for the duration of the scope in which it is introduced.

Example

#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

See also

Template:cpp/language/dcl list namespace