namespace Declaration
C++ Specific —>
namespace [identifier] { namespace-body }
A namespace declaration identifies and assigns a name to a declarative region.
The identifier in a namespace declaration must be unique in the declarative region in which it is used. The identifier is the name of the namespace and is used to reference its members.
The declarative region of a namespace declaration is its namespace-body.
END C++ Specific
Namespace Alias
A namespace-alias is an alternative name for a namespace.
Syntax
namespace-alias :
identifier
namespace-alias-definition :
namespace identifier = qualified-namespace-specifier;
qualified-namespace-specifier :
::opt nested-name-specifieropt class-or-namespace-name
A namespace-alias-definition declares an alternate name for a namespace. The identifier is a synonym for the qualified-namespace-specifier and becomes a namespace-alias. For example:
namespace a_very_long_namespace_name { ... }
namespace AVLNN = a_very_long_namespace_name;
// AVLNN is now a namespace-alias for a_very_long_namespace_name.
A namespace-name cannot be identical to any other entity in the same declarative region. In addition, a global namespace-name cannot be the same as any other global entity name in a given program.
namespace Declaration
A namespace declaration identifies and assigns a name to a declarative region.
Syntax
original-namespace-name :
identifier
namespace-definition :
original-namespace-definition
extension-namespace-definition
unnamed-namespace-definition
original-namespace-definition :
namespace identifier { namespace-body }
extension-namespace-definition :
namespace original-namespace-name { namespace-body }
unnamed-namespace-definition :
namespace { namespace-body }
namespace-body :
declaration-seqopt
The identifier in an original-namespace-definition must be unique in the declarative region in which it is used. The identifier is the name of the namespace and is used to reference its members. Subsequently, in that declarative region, it is treated as an original-namespace-name.
The declarative region of a namespace-definition is its namespace-body.
A namespace can contain data and function declarations. The declaration-seq is a list of these declarations which are said to be members of the namespace.
Defining namespace Members
Members of a namespace may be defined within that namespace. For example:
namespace X { void f() { } }
Members of a named namespace can be defined outside the namespace in which they are declared by explicit qualification of the name being defined. However, the entity being defined must already be declared in the namespace. In addition, the definition must appear after the point of declaration in a namespace that encloses the declaration’s namespace. For example:
namespace Q {
namespace V {
void f();
}
void V::f() { } // ok
void V::g() { } // error, g() is not yet a member of V
namespace V {
void g();
}
}
Unnamed namespaces
An unnamed-namespace-definition behaves as if it were replaced by:
namespace unique { namespace-body }
using namespace unique;
Each unnamed namespace has an identifier, represented by unique, that differs from all other identifiers in the entire program. For example:
namespace { int i; } // unique::i
void f() { i++; } // unique::i++
namespace A {
namespace {
int I; // A::unique::i
int j; // A::unique::j
}
}
using namespace A;
void h()
{
I++; //error: unique::i or A::unique::i
A::i++; // error: A::i undefined
j++; // A::unique::j++
}
Unnamed namespaces are a superior replacement for the static declaration of variables. They allow variables and functions to be visible within an entire translation unit, yet not visible externally. Although entities in an unnamed namespace might have external linkage, they are effectively qualified by a name unique to their translation unit and therefore can never be seen from any other translation unit.