A namespace in C++ is a named scope that groups related functions, classes, variables and other names so they cannot collide with identical names elsewhere. You declare one with the namespace keyword and reach its members with the scope resolution operator ::, for example std::vector. Namespaces cost nothing at run time: they are resolved entirely at compile time.
Modern C++ (C++17 and C++20) adds nested namespace shorthand, inline namespaces, and modules, which complement namespaces.
A namespace can be reopened in the same or another file to add more members, which is how the standard library spreads std across many headers.
You have two ways to avoid repeating the prefix.
Prefer a using declaration for specific names inside a function. Never put using namespace std; in a header file: it forces every includer to accept all of std and invites ambiguities.
Since C++17 you can write nested namespaces on one line.
Aliases shorten long names without importing them. In C++20 and later, using enum can also bring enumerators into scope, which is a related feature for scoped enums.
Names inside an unnamed (anonymous) namespace are visible only in the current translation unit. This is the C++ replacement for file-level static.
An inline namespace makes its members available in the enclosing namespace, which lets a library ship several versions and select a default.
api::name() resolves to the inline namespace v2, while api::v1::name() stays reachable explicitly.
When you call a function unqualified, C++ also searches the namespaces of the argument types. This is why operator<< for a user type in its own namespace is found without a prefix.
Keep a type's operators and helper functions in the same namespace as the type so lookup works.
std; this is undefined behaviour except for specific allowed specialisations.using namespace at global scope in headers.static) for file-private helpers.} // namespace math_ops for readability.A namespace only groups names and cannot be instantiated or have access control. A class defines a type with members, access specifiers and objects.
using namespace std; hurt performance?No, it affects only name lookup at compile time. The problem is name clashes and readability, not speed.
Yes. Any number of headers and source files can contribute members to the same namespace.
No. Modules change how code is imported and exported, but namespaces still organise names inside modules.
C++ namespaces group names under a scope to prevent collisions and organise code. Use :: qualification, targeted using declarations, C++17 nested namespaces, unnamed namespaces for file-private code and inline namespaces for versioning. Avoid broad using namespace directives in headers and keep related operators beside their types for argument-dependent lookup.