---
title: "Namespaces (C++) | Microsoft Docs"
ms.custom: ""
ms.date: "08/30/2017"
ms.reviewer: ""
ms.suite: ""
ms.technology: ["cpp-language"]
ms.tgt_pltfrm: ""
ms.topic: "language-reference"
f1_keywords: ["namespace_CPP", "using_CPP"]
dev_langs: ["C++"]
helpviewer_keywords: ["namespaces [C++], C++", "namespaces [C++]", "namespaces [C++], global", "global namespace", "Visual C++, namespaces"]
ms.assetid: d1a5a9ab-1cad-47e6-a82d-385bb77f4188
caps.latest.revision: 14
author: "mikeblome"
ms.author: "mblome"
manager: "ghogen"
ms.workload: ["cplusplus"]
---
# Namespaces (C++)
A namespace is a declarative region that provides a scope to the identifiers (the names of types, functions, variables, etc) inside it. Namespaces are used to organize code into logical groups and to prevent name collisions that can occur especially when your code base includes multiple libraries. All identifiers at namespace scope are visible to one another without qualification. Identifiers outside the namespace can access the members by using the fully qualified name for each identifier, for example `std::vector<:string> vec;`, or else by a [using Declaration](../cpp/using-declaration.md) for a single identifier (`using std::string`), or a [using Directive](../cpp/namespaces-cpp.md#using_directives) for all the identifiers in the namespace (`using namespace std;`). Code in header files should always use the fully qualified namespace name.
The following example shows a namespace declaration and three ways that code outside the namespace can accesses their members.
```cpp
namespace ContosoData
{
class ObjectManager
{
public:
void DoSomething() {}
};
void Func(ObjectManager) {}
}
```
Use the fully qualified name:
```cpp
ContosoData::ObjectManager mgr;
mgr.DoSomething();
ContosoData::Func(mgr);
```
Use a using declaration to bring one identifier into scope:
```cpp
using ContosoData::ObjectManager;
ObjectManager mgr;
mgr.DoSomething();
```
Use a using directive to bring everything in the namespace into scope:
```cpp
using namespace ContosoData;
ObjectManager mgr;
mgr.DoSomething();
Func(mgr);
```
## using directives
The `using` directive allows all the names in a **namespace** to be used without the *namespace-name* as an explicit qualifier. Use a using directive in an implementation file (i.e. *.cpp) if you are using several different identifiers in a namespace; if you are just using one or two identifiers, then consider a using declaration to only bring those identifiers into scope and not all the identifiers in the namespace. If a local variable has the same name as a namespace variable, the namespace variable is hidden. It is an error to have a namespace variable with the same name as a global variable.
> [!NOTE]
> A using directive can be placed at the top of a .cpp file (at file scope), or inside a class or function definition.
>
> In general, avoid putting using directives in header files (*.h) because any file that includes that header will bring everything in the namespace into scope, which can cause name hiding and name collision problems that are very difficult to debug. Always use fully qualified names in a header file. If those names get too long, you can use a namespace alias to shorten them. (See below.)
## Declaring namespaces and namespace members
Typically, you declare a namespace in a header file. If your function implementations are in a separate file, then qualify the function names, as in this example.
```cpp
//contosoData.h
#pragma once
namespace ContosoDataServer
{
void Foo();
int Bar();
}
```
Function implementations in contosodata.cpp should use the fully qualified name, even if you place a `using` directive at the top of the file:
```cpp
#include "contosodata.h"
using namespace ContosoDataServer;
void ContosoDataServer::Foo() // use fully-qualified name here
{
// no qualification needed for Bar()
Bar();
}
int ContosoDataServer::Bar(){return 0;}
```
A namespace can be declared in multiple blocks in a single file, and in multiple files. The compiler joins the parts together during preprocessing and the resulting namespace contains all the members declared in all the parts. An example of this is the std namespace which is declared in each of the header files in the standard library.
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 definition must appear after the point of declaration in a namespace that encloses the declaration's namespace. For example:
```cpp
// defining_namespace_members.cpp
// C2039 expected
namespace V {
void f();
}
void V::f() { } // ok
void V::g() { } // C2039, g() is not yet a member of V
namespace V {
void g();
}
}
```
This error can occur when namespace members are declared across multiple header files, and you have not included those headers in the correct order.
## The global namespace
If an identifier is not declared in an explicit namespace, it is part of the implicit global namespace. In general, try to avoid making declarations at global scope when possible, except for the entry point [main Function](../c-language/main-function-and-program-execution.md), which is required to be in the global namespace. To explicitly qualify a global identifier, use the scope resolution operator with no name, as in `::SomeFunction(x);`. This will differentiate the identifier from anything with the same name in any other namespace, and it will also help to make your code easier for others to understand.
## The std namespace
All C++ standard library types and functions are declared in the `std` namespace or namespaces nested inside `std`.
## Nested namespaces
Namespaces may be nested. An ordinary nested namespace has unqualified access to its parentâs members, but the parent members do not have unqualified access to the nested namespace (unless it is declared as inline), as shown in the following example:
```cpp
namespace ContosoDataServer
{
void Foo();
namespace Details
{
int CountImpl;
void Ban() { return Foo(); }
}
int Bar(){...};
int Baz(int i) { return Details::CountImpl; }
}
```
Ordinary nested namespaces can be used to encapsulate internal implementation details that are not part of the public interface of the parent namespace.
## Inline namespaces (C++ 11)
In contrast to an ordinary nested namespace, members of an inline namespace are treated as members of the parent namespace. This characteristic enables argument dependent lookup on overloaded functions to work on functions that have overloads in a parent and a nested inline namespace. It also enables you to declare a specialization in a parent namespace for a template that is declared in the inline namespace. The following example shows how external code binds to the inline namespace by default:
```cpp
//Header.h
#include