Namespaces
Variants
Views
Actions

Namespace aliases

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

 
 
C++ language
General topics
Flow control
Conditional execution statements
Iteration statements
Jump statements
Functions
function declaration
lambda function declaration
function template
inline specifier
exception specifications (deprecated)
noexcept specifier (C++11)
Exceptions
Namespaces
namespace aliases
Types
decltype specifier (C++11)
Specifiers
cv specifiers
storage duration specifiers
constexpr specifier (C++11)
auto specifier (C++11)
alignas specifier (C++11)
Initialization
Literals
Expressions
alternative representations
Utilities
Types
typedef declaration
type alias declaration (C++11)
attributes (C++11)
Casts
implicit conversions
const_cast conversion
static_cast conversion
dynamic_cast conversion
reinterpret_cast conversion
C-style and functional cast
Memory allocation
Classes
Class-specific function properties
Special member functions
Templates
class template
function template
template specialization
parameter packs (C++11)
Miscellaneous
Inline assembly
 

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