Namespaces
From cppreference.com
Namespaces provide a method for preventing name conflicts in large projects.
Symbols declared inside a namespace block are placed in a named scope that prevents them from being mistaken for identically-named symbols in other scopes.
Multiple declarations of namespaces with the same name are allowed, resulting in a namespace including all symbols from all such declarations.
Contents |
[edit] Syntax
namespace ns_name { declarations } | (1) | ||||||||
inline namespace ns_name { declarations } | (2) | (since C++11) | |||||||
ns_name::name | (3) | ||||||||
using namespace ns_name; | (4) | ||||||||
using ns_name::name; | (5) | ||||||||
[edit] Explanation
This section is incomplete |
- Declaration of the namespace name.
- Declaration of the namespace name. Definitions will appear both inside name and its enclosing namespace
- Standard way of accessing namespace content.
- Making all symbols of a namespace accessible in the scope of the using directive.
- Making a specific symbols of a namespace accessible in the scope of the using directive.
[edit] Example
This example shows how to use a namespace to create a class that already has been named in the std namespace.
#include <vector> namespace vec { template< typename T > class vector { // ... }; } // of vec int main() { std::vector<int> v1; // Standard vector. vec::vector<int> v2; // User defined vector. v1 = v2; // Error: v1 and v2 are different object's type. { using namespace std; vector<int> v3; // Same as std::vector v1 = v3; // OK } { using vec::vector; vector<int> v4; // Same as vec::vector v2 = v4; // OK } return 0; }
[edit] See also
namespace alias | creates an alias of an existing namespace |