Content from C + + primer 5th
Large programs often use several independently developed libraries, which in turn define a large number of global names, inevitably with name collisions.
Namespaces: Provides a more manageable mechanism for preventing name collisions. Namespaces are separated by a global namespace, where each namespace is a scope. By defining the name of a library in a namespace, you can avoid collisions of global names.
Defined:
Namespace space_name{
declarations and definitions, as long as they can appear in the global scope, can be in the namespace
Mainly classes, variables, functions, templates, other namespaces
}
The namespace ends without semicolons.
The name of the namespace must also remain unique within the scope in which it is defined.
Namespaces cannot be defined inside classes and functions.
Each namespace is a scope.
The definition of a namespace can be divided into several parts without having to define it once.
Like what:
Namespace nsp{
}
It is possible to define a new namespace named NSP, or to add some new members to a namespace that already exists.
Template specializations must be defined in the namespace to which the original template belongs.
inline namespaces
C++11 New Standard
Unlike normal nested namespaces, names in an inline namespace can be used directly by the outer namespace, that is, without prefixing the namespace with the name of the inline namespace.
Inline namespace space_name{
}
Inline must appear in the namespace where it was first defined and can be written inline or not when the namespace is opened later.
Unnamed namespaces
Refers to the keyword namespace directly behind {}
Variables defined in an unnamed namespace have a static life cycle: Created before the first use, until the end of the program is destroyed.
NOTES: C + + namespaces