namespace keyword | Declares namespace |
declaration := namespace-defn block-decl := namespace-alias-defn using-directive namespace-defn ::= named-namespace-defn unnamed-namespace-defn named-namespace-defn ::= namespace identifier { namespace-body } unnamed-namespace-defn ::= namespace { namespace-body } namespace-body ::= [ declaration-seq ] namespace-alias-defn ::= namespace identifier = namespace-specifier ; namespace-specifier ::= [::] [ nested-name ::] namespace-name namespace-name ::= identifier using-directive ::= using namespace namespace-specifier ; |
The namespace keyword can be used in a namespace definition, a namespace alias definition, or a using directive. A namespace is a scope for declarations of classes, templates, functions, objects, and other namespaces. Outside a namespace, you can refer to a name that is declared in the namespace by qualifying the name with the scope operator ( : :), such as ns::name , or with a using directive or declaration.
Multiple namespace declarations can name the same namespace, each one adding more declarations to the namespace. The standard namespace, std , is built this way, with many different headers all placing their declarations in the std namespace. A namespace can be anonymous, which prevents the enclosed declarations from being visible in other source files.
A namespace alias defines an identifier as a synonym for an existing namespace. See using for information on the using directive.
namespace math_version_2 { const long double pi = 3.1415926535897932385L; }; namespace math = math_version_2; using namespace math;
class , declaration , identifier , using , Chapter 2