Namespaces
Variants
Views
Actions

Namespaces

From cppreference.com
< cpp‎ | language
Revision as of 06:09, 4 December 2012 by Cubbi (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 declaration
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
 

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

Syntax

namespace Template:sparam { Template:sparam } (1)
inline namespace Template:sparam { Template:sparam } (2) (since C++11)
namespace { Template:sparam } (3)
Template:sparam::Template:sparam (4)
using namespace Template:sparam; (5)
using Template:sparam::Template:sparam; (6)

Explanation

1) Declaration of the namespace Template:sparam.
2) Declaration of the namespace Template:sparam. Definitions will appear both inside Template:sparam and its enclosing namespace.
3) Unnamed namespace. Definitions have potential scope from their point of declaration to the end of the translation unit.
4) Standard way of accessing namespace content.
5) Making all symbols of a namespace accessible in the scope of the using directive.
6) Making a specific symbols of a namespace accessible in the scope of the using directive.

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;
}


See also

Template:cpp/language/dcl list namespace alias