On this page
  1. Namespaces
  2. Overview
  3. Notes & Pitfalls
  4. Example

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

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