On this page
Namespaces
Prev: Classes, Inheritance & virtual | Up: builtins | Next: sizeof & alignof
Rel: Compilation, Headers, Modules & ABI
Ref: cppreference — Namespaces
Tags: #public #prog #cpp #builtins #tier/standard
Overview
Namespaces partition the global symbol space to prevent name collisions across libraries. Anonymous namespaces (namespace { ... }) give definitions internal linkage within a single .cpp translation unit—the modern C++ replacement for file-scope static.
Notes & Pitfalls
- Never put
using namespace std;in header files: It pollutes every translation unit that includes your header and causes silent name collisions. - Use anonymous
namespace { ... }in.cppfiles for file-private helpers (internal linkage), and C++17 nestednamespace app::net {}.
Example
#include <cassert>
namespace app::math {
int square(int x) { return x * x; }
}
int main() {
namespace m = app::math; // local namespace alias
assert(m::square(5) == 25);
}
Linked from
Related notes
- tuts/cpp/builtins/classes_and_virtual Linked here; links to this page; shared tags: #builtins, #cpp, #prog, #tier
- tuts/cpp/builtins/sizeof_and_alignof Linked here; links to this page; shared tags: #builtins, #cpp, #prog, #tier
- tuts/cpp/builtins Linked here; links to this page; shared tags: #builtins, #cpp, #prog
- tuts/cpp/language_and_raii/compilation_headers_modules_and_abi Linked here; links to this page; shared tags: #cpp, #prog, #tier
- tuts/cpp/builtins/auto shared tags: #builtins, #cpp, #prog, #tier
- tuts/cpp/builtins/c_style_casts shared tags: #builtins, #cpp, #prog, #tier
- tuts/cpp/builtins/casts shared tags: #builtins, #cpp, #prog, #tier
- tuts/cpp/builtins/concepts shared tags: #builtins, #cpp, #prog, #tier
- tuts/cpp/builtins/constexpr shared tags: #builtins, #cpp, #prog, #tier
- tuts/cpp/builtins/lambdas shared tags: #builtins, #cpp, #prog, #tier