Namespaces
A namespace is a named scope for declarations. It lets two libraries both define a Logger or a sort without colliding, because the full name of each includes the namespace it lives in.
Detailed Explanation
Every function, type, and global variable needs a name, and a program links them all together into one symbol table. Pull in a JSON library and an HTTP library and the odds that both define Buffer are high. Before namespaces, C and early C++ solved this by prefixing everything by hand: png_read_info, curl_easy_setopt, MyCompanyStringUtils. The prefix was documentation-by-convention, and nothing stopped two vendors picking the same one.
A namespace makes the prefix a language feature. Declarations inside namespace codephataa { ... } have qualified names beginning codephataa::, and that qualification is encoded into the linker symbol, so codephataa::Buffer and vendor::Buffer are genuinely different entities. The whole standard library sits in std for this reason.
Namespaces are open. You can reopen the same namespace in as many files as you like and each block adds to it — that is how a library's declarations are spread across dozens of headers. They also nest. Before C++17 you wrote three lines of opening braces; C++17 lets you write namespace codephataa::net::detail { ... } in one. A widespread convention is a detail or internal nested namespace for things that are public to the library but not part of its supported interface.
An anonymous namespace — namespace { ... } with no name — behaves differently. Its contents get a compiler-generated unique name and internal linkage, meaning they are invisible outside the current translation unit. This is the modern replacement for file-scope static, and unlike static it works for types as well as functions and variables.
An inline namespace is the versioning tool. Names inside an inline namespace are also visible in the enclosing namespace, so codephataa::v2::parse can be called as codephataa::parse while codephataa::v1::parse remains reachable for old code. Because the mangled symbol still contains v2, an object file built against v1 will not silently link against v2 — you get an honest undefined reference instead of a runtime surprise. Libstdc++ uses exactly this for its ABI transitions.
Then there is argument-dependent lookup, or ADL, sometimes called Koenig lookup. When you call an unqualified function, the compiler searches not only the enclosing scopes but also the namespaces of the argument types. Call parse(cfg) where cfg is a codephataa::Config, and the compiler looks inside codephataa even though you never said so.
ADL is not an oddity you can ignore. It is what makes std::cout << x work: operator<< lives in std, and you never write std::operator<<. It is what makes the swap idiom work — you write using std::swap; swap(a, b); so that a type's own swap in its own namespace wins if one exists, falling back to std::swap otherwise. The rule to take away: a free function intended as part of a type's interface should live in the same namespace as that type.
ADL also surprises people. Adding a function to a namespace can change which overload an existing unqualified call resolves to, in a different file, with no warning. When that matters, qualify the call, or wrap it in parentheses — (parse)(cfg) suppresses ADL entirely.
Code Examples
#include <iostream>
#include <string>
namespace codephataa::net { // C++17 nested namespace, one line
struct Request { std::string path; };
// Free function in the same namespace as its type: found by ADL.
void log(const Request& r) { std::cout << "GET " << r.path << '\n'; }
namespace detail { // implementation, not part of the interface
bool isSafePath(const std::string& p) { return p.find("..") == std::string::npos; }
}
}
int main() {
codephataa::net::Request r{"/health"};
log(r); // unqualified: ADL finds codephataa::net::log
// prints: GET /health
}#include <utility>
#include <vector>
namespace geometry {
struct Point { int x, y; };
// A cheaper swap than the generic one for types where it matters.
void swap(Point& a, Point& b) noexcept { std::swap(a.x, b.x); std::swap(a.y, b.y); }
}
template <typename T>
void rotateLeft(std::vector<T>& v) {
using std::swap; // fallback candidate
for (std::size_t i = 0; i + 1 < v.size(); ++i)
swap(v[i], v[i + 1]); // ADL prefers geometry::swap for Point
}Versioning with an inline namespace: codephataa::encode resolves to v2::encode,
// Versioning with an inline namespace: codephataa::encode resolves to v2::encode,
// while codephataa::v1::encode stays callable for code that has not migrated.
namespace codephataa {
namespace v1 { int encode(int x) { return x * 2; } }
inline namespace v2 { int encode(int x) { return x * 3; } }
}
int a = codephataa::encode(4); // 12, calls v2
int b = codephataa::v1::encode(4); // 8Common Gotchas
`using namespace std;` at file scope in a header
it forces the whole standard library into every file that includes the header, transitively. Your `count` or `distance` or `data` will one day collide with `std::` and the error message will be a wall of template text. Keep it out of headers entirely and out of the global scope of source files.
ADL can pull in an overload you never intended
adding a free function to a namespace changes the candidate set for unqualified calls elsewhere in the program. If a call must resolve to a specific function, qualify it.
Namespaces do not control access
anything in a `detail` namespace is fully reachable by anyone who types the name. It signals intent, not enforcement. Use an anonymous namespace or a private header if you need genuine hiding.
Namespace aliases keep long names readable
`namespace fs = std::filesystem;` is fine in a source file and inside a function. In a header it leaks the alias to every includer, so scope it as tightly as you can.