A namespace is a named scope that groups related declarations (functions, classes, variables) to prevent name collisions between different libraries or modules. Qualifying names with the namespace (ns::name) disambiguates them. Namespaces are essential for organizing large codebases and avoiding clashes with third-party or standard-library names.
namespace math {
int add(int a, int b) { return a + b; }
}
int r = math::add(2, 3);
Real-world example
Two libraries each define a Logger class; putting them in namespaces avoids the collision.
Likely follow-ups
How do namespaces prevent collisions? | How do you access a name in a namespace?
using namespace std; brings all names from std into the current scope so you can omit the std:: prefix. It's discouraged, especially in headers, because it pollutes the global/namespace scope everywhere the header is included, risking name collisions and ambiguities. Prefer explicit std:: or targeted using-declarations in narrow scopes.
using std::cout; // targeted, safer
// avoid: using namespace std; in headers
Real-world example
A using namespace std in a widely-included header causes a surprising ambiguity with a user 'count' function.
Likely follow-ups
Why is it worse in headers? | What is a safer alternative?
A using-declaration (using std::vector;) brings a single name into scope. A using-directive (using namespace std;) brings all names from a namespace into scope. Using-declarations are more precise and safer; using-directives are broad and collision-prone. Prefer using-declarations, scoped as narrowly as possible.
using std::string; // declaration: just string
using namespace std; // directive: everything in std
Real-world example
A function uses a targeted using-declaration for std::swap to enable ADL while keeping scope clean.
Likely follow-ups
Which is more precise? | Why prefer using-declarations?
An unnamed namespace gives its contents internal linkage—they're visible only within the current translation unit, like static at file scope but working for types too. It's the modern C++ way to make helper functions, constants, or types local to a .cpp file, avoiding linker collisions with same-named symbols elsewhere.
ADL (Koenig lookup) means that when calling an unqualified function, the compiler also searches the namespaces of the argument types for matching functions. This lets operators and functions like swap or begin be found in the type's own namespace without qualification, which is why 'using std::swap; swap(a,b);' enables customized swaps.
Namespaces can be nested to reflect module structure. C++17 added a compact syntax using :: to declare nested namespaces in one line, avoiding multiple nested braces. Names are accessed with the full qualified path (outer::inner::name).
namespace a::b::c { // C++17 nested shorthand
int x;
}
int y = a::b::c::x;
Real-world example
A project uses nested namespaces like company::product::detail to organize its code.
Likely follow-ups
What did C++17 simplify? | How do you access a deeply nested name?
A namespace alias creates a shorter name for a long or nested namespace, improving readability without a broad using-directive. It's commonly used for deeply nested namespaces or to switch implementations by aliasing one namespace to a chosen name.
namespace fs = std::filesystem;
fs::path p = "/tmp";
Real-world example
Aliasing std::filesystem to fs keeps code concise while remaining explicit.
Likely follow-ups
How does an alias differ from a using-directive? | When is it useful?
An inline namespace's members are treated as if they were in the enclosing namespace (accessible without the inner qualifier), while still being distinct for linkage/ABI. It's primarily used for library versioning: a library can put its current API in an inline namespace (e.g., v2) so users write ns::name but the symbols are versioned for ABI compatibility.
The global namespace is the outermost scope, containing names not placed in any namespace. You refer to a global name explicitly with a leading :: (e.g., ::func), which is useful to disambiguate from a same-named name in the current namespace. Minimizing what lives in the global namespace reduces collisions.
::printf("hi"); // global-scope function explicitly
Real-world example
A leading :: disambiguates the global ::size from a member or namespace-scoped size.
Likely follow-ups
How do you access a global name explicitly? | Why limit global-namespace usage?
Namespaces partition names by module/library/component, preventing collisions, communicating ownership and structure, and allowing a 'detail' sub-namespace for internal helpers not meant for public use. Combined with headers and build structure, they make large projects navigable and reduce coupling between components.