Cheat Sheet - Linux: /linux/ - CLI: /linux/cli/ - locate vs type vs which vs whereis: /linux/cli/locate-vs-type-vs-which-vs-whereis/ - make: /linux/cli/make/ - awk: /linux/cli/awk/ - find: /linux/cli/find/ - grep: /linux/cli/grep/ - pgrep: /linux/cli/pgrep/ - ps: /linux/cli/ps/ - pstree: /linux/cli/pstree/ - trap: /linux/cli/trap/ - xargs: /linux/cli/xargs/ - Shell: /linux/shell/ - BASH_SOURCE: /linux/shell/bash-source/ - Redirection: /linux/shell/redirection/ - Special Variables: /linux/shell/special-variables/ - Scripting Tips: /linux/shell/script-tips/ - set -o vi: /linux/shell/bash-vi-mode/ - Shell Processing Order: /linux/shell/process-order/ - What are Interactive and Login Shells?: /linux/shell/interactive-login/ - Double Quote Expansion: /linux/shell/shell-expansion/ - System Programming: /linux/system-programming/ - File I/O: /linux/system-programming/file-io/ - Process: /linux/system-programming/process/ - Shared Libraries: /linux/system-programming/shared-libraries/ - Kernel: /linux/kernel/ - Go: /programming/go/ - Concurrency: /programming/go/concurrency/ - CSP (Communicating Sequential Processes) and Channels: /programming/go/concurrency/csp/ - Channel: /programming/go/concurrency/channel/ - Select: /programming/go/concurrency/select/ - sync: /programming/go/concurrency/sync/ - Concurrency Patterns: /programming/go/concurrency/patterns/ - Goroutines and the Go Runtime: /programming/go/concurrency/runtime/ - Standard Library: /programming/go/std/ - os: /programming/go/std/os/ - context: /programming/go/std/context/ - io: /programming/go/std/io/ - json: /programming/go/std/json/ - net/http: /programming/go/std/net-http/ - slog: /programming/go/std/slog/ - testing: /programming/go/std/testing/ - time: /programming/go/std/time/ - Ellipsis (...): /programming/go/ellipsis/ - error: /programming/go/error/ - Function: /programming/go/function/ - Go Tool: /programming/go/tool/ - Interface: /programming/go/interface/ - Map: /programming/go/map/ - Package and Module: /programming/go/package-module/ - Pointer: /programming/go/pointer/ - Slice: /programming/go/slice/ - String: /programming/go/string/ - Switch: /programming/go/switch/ - Comparison: /programming/go/comparison/ - defaultdict: /programming/python/collections/defaultdict/ - heapq: /programming/python/collections/heapq/ - @cache: /programming/python/cache/ - abc: /programming/python/abc/ - bisect: /programming/python/bisect/ - Type Hints: /programming/python/type-hints/ - Passing Mechanism: /programming/python/passing-mechanism/ - uv: /programming/python/uv/ - *args and **kwargs: /programming/python/args-kwargs/ - re: /programming/python/re/ - C: /programming/c/ - Pointers and Arrays: /programming/c/pointer/ - Syntax Basics: /programming/c/syntax-basics/ - static: /programming/c/static/ - Bit Manipulation: /programming/c/bit-manipulation/ - C vs C++: /programming/c/c-vs-cpp/ - stdatomic: /programming/c/stdatomic/ - C++: /programming/cpp/ - RAII (Resource Acquisition Is Initialization): /programming/cpp/raii/ - Copy Elision & Move Semantics: /programming/cpp/copy-elision-and-move-semantics/ - Most Vexing Parse & In-Class Initializer: /programming/cpp/most-vexing-parse/ - C++ Keywords Reference: /programming/cpp/keywords/ - C++ Primer: /programming/cpp/primer/ - Variables and Basic Types: /programming/cpp/primer/variables-and-basic-types/ - Expressions: /programming/cpp/primer/expressions/ - Type Conversions and Casts: /programming/cpp/primer/type-conversions-and-casts/ - Statements: /programming/cpp/primer/statements/ - Functions: /programming/cpp/primer/functions/ - Classes: /programming/cpp/primer/classes/ - The IO Library: /programming/cpp/primer/io-library/ - STL: /programming/cpp/primer/stl/ - Lambda Expressions: /programming/cpp/primer/lambda/ - Smart Pointers: /programming/cpp/primer/smart-pointers/ - Copy Control: /programming/cpp/primer/copy-control/ - Overloaded Operations and Conversions: /programming/cpp/primer/operator-overloading/ - Object-Oriented Programming (OOP): /programming/cpp/primer/oop/ - Templates and Generic Programming: /programming/cpp/primer/templates/ - Namespaces: /programming/cpp/primer/namespaces/ - Pointers to Class Member: /programming/cpp/primer/pointers-to-class-member/ - Advanced: /programming/cpp/advanced/ - Error Handling: /programming/cpp/advanced/error-handling/ - Concurrency & Threading: /programming/cpp/advanced/concurrency/ - Unit Testing: /programming/cpp/advanced/unit-testing/ - Comparison: /programming/comparison/ - Kubernetes: /cloud-devops/kubernetes/ - Fleet: /cloud-devops/kubernetes/gke/fleet/ - Kustomize vs Helm: /cloud-devops/kubernetes/kustomize-vs-helm/ - API Extension: /cloud-devops/kubernetes/api-extension/ - Concept: /cloud-devops/kubernetes/kueue/concept/ - DRA: /cloud-devops/kubernetes/dra/ - Secret Manager: /cloud-devops/terraform/google-secret-manager/ - Knowledge: /network/knowledge/ - PDU: /network/knowledge/osi-model/ - curl: /network/cli/curl/ - Concurrency Concepts: /cs/concurrency-and-parallelism/concepts/ - Concurrency vs Parallelism: /cs/concurrency-and-parallelism/concurrency-vs-parallelism/ - Mutex vs Atomic: /cs/concurrency-and-parallelism/mutex-vs-atomic/ - Regular Expression: /cs/regular-expression/ - Multiplexer vs Dispatcher: /cs/multiplexer-vs-dispatcher/ - Driver and Runtime: /ai-ml/gpu/driver-and-runtime/ - Serving: /ai-ml/serving/ - Dotfiles: /dotfiles/ - Git: /dotfiles/git/ - Cheat Sheet: /dotfiles/git/cheat-sheet/ - revisions: /dotfiles/git/revisions/ - Submodule vs Worktree: /dotfiles/git/submodule-vs-worktree/ - checkout, reset -> switch, restore: /dotfiles/git/checkout-switch-restore-reset/ - rebase: /dotfiles/git/rebase/ - Branching and Merging: /dotfiles/git/branching-merging/ - Vim: /dotfiles/vim/ - Plugin: /dotfiles/vim/plugin/ - auto-pairs: /dotfiles/vim/plugin/auto-pairs/ - vim-commentary: /dotfiles/vim/plugin/vim-commentary/ - vim-easymotion: /dotfiles/vim/plugin/vim-easymotion/ - vim-surround: /dotfiles/vim/plugin/vim-surround/ - Marks: /dotfiles/vim/tips/marks/ - vimrc: /dotfiles/vim/vimrc/ - lf: /dotfiles/lf/ # C++ Keywords Reference This post consolidates core C++ and Modern C++ (C++11+) keywords, organized by role. ### Anatomy of Keyword Placement in a Function Declaration Keywords attached to a C++ function declaration are placed into **Declaration Specifiers (prefix) / Function Qualifiers & Specifiers (after `()`) / Function Body (at the end)** according to the C++ grammar and compiler parsing order: ```text virtual const int& FUNCTION(PARAM_TYPE PARAM_NAME) const & noexcept = 0; ─────── ────────── ─────────────────────────────── ──────────────── ─── │ │ │ │ └─ Function Body: { ... }, = default, = delete, = 0 │ │ │ └─ Function Qualifiers & Specifiers: const/volatile, &/&&, noexcept, override, final │ │ └─ Function Name(Params) │ └─ Return Type (prefix const = const return value) └─ Declaration Specifiers: explicit, virtual, inline, static, constexpr ``` - **1. Declaration Specifiers (before function: `explicit`, `virtual`, `inline`, `static`, `constexpr`):** - Declare **how the function is linked, inlined, or invoked** before reaching the function name, independent of its parameters or body. - **2. Function Qualifiers & Specifiers (after `()`: `const`/`volatile`, `&`/`&&`, `noexcept`, `override`, `final`):** - **Avoiding Return-Type Ambiguity (`const`, `&` / `&&`):** Writing `const` or `&`/`&&` in front (`const int& func()`) qualifies the **return type**, not the member function. Placing them after `()` unambiguously qualifies the implicit `*this` object's constness (`const`) and value category (`&` = lvalue, `&&` = rvalue) *(see [Copy Elision & Move Semantics: Ref-qualified Member Functions]({{< relref "/programming/cpp/copy-elision-and-move-semantics.md#6-ref-qualified-member-functions" >}}))*. - **Referencing Parameter Scope (`noexcept`):** The compiler parses left-to-right, and conditional exception specifications `noexcept(expr)` **must be able to reference parameter names** (e.g., `void f(T x) noexcept(x.is_safe())`). Thus, `noexcept` must appear after the parameter list `( ... )` has been declared. - **3. Function Body & Pure Specifier (at the end after `=`: `= default`, `= delete`, `= 0`):** - **Replacing `{ ... }`:** These stand directly in place of the function body `{ ... }` at the very end of the declaration. - **Why `=` is used:** Following the classic pure virtual syntax (`= 0`), C++11 **reused existing keywords (`default` from `switch` and `delete` from memory deallocation) after `=`** to avoid introducing new reserved words while clearly signaling a special function-body definition. --- ## 1. Special Member Function & Conversion Control Keywords (`= default`, `= delete`, `explicit`, `noexcept`) These keywords control constructors, destructors, copy/move operations, and type conversions at compile time (see [Copy Control]({{< relref "/programming/cpp/primer/copy-control.md" >}}) and [Copy Elision & Move Semantics]({{< relref "/programming/cpp/copy-elision-and-move-semantics.md" >}})). | Keyword | Target | Meaning & Core Role | | :--- | :--- | :--- | | **`= default`** | Default constructor & the 5 copy-control special member functions | Explicitly instructs the compiler to **generate the synthesized default implementation** (memberwise initialization/copy/move/destruction). Implicitly `inline` when written on the in-class declaration. | | **`= delete`** | Any function (primarily copy constructor & copy assignment) | **Prohibits calling the function at compile time.** Used for non-copyable types like `std::unique_ptr` and `std::iostream`. | | **`explicit`** | Single-argument constructors or conversion operators (`operator TYPE()`, e.g., `operator int()`, `operator bool()`) | **Blocks unintended implicit type conversions and copy initialization (`=`)**, allowing only direct initialization (`CLASS x(val);`) and explicit casts (`static_cast`).
*(Exception: `explicit operator bool() const` blocks arithmetic conversions but is still automatically applied in condition contexts like `if`, `while`, `!`, `&&`, `\|\|`, and `?:`.)* | | **`noexcept`** | Move constructor, move assignment, destructor, ordinary functions | Guarantees to the compiler and standard library that the function **will never throw an exception**. If an exception escapes a `noexcept` function, **`std::terminate()` is called immediately** without stack unwinding.
*(For its role in conditional move optimization across standard containers and algorithms, see [Copy Elision & Move Semantics]({{< relref "/programming/cpp/copy-elision-and-move-semantics.md#3-always-mark-both-declaration-and-definition-noexcept" >}}))* | ```cpp class Resource { public: Resource() = default; // Use synthesized default constructor explicit Resource(int size); // Disallow implicit int -> Resource conversion (Resource r = 10; is an error!) Resource(const Resource&) = delete; // No copy constructor Resource& operator=(const Resource&) = delete; // No copy assignment Resource(Resource&&) noexcept = default; // Non-throwing move constructor Resource& operator=(Resource&&) noexcept = default;// Non-throwing move assignment explicit operator bool() const noexcept { // Auto-converts to bool only in condition contexts like if (res) return valid_; } ~Resource() = default; private: bool valid_ = false; }; ``` --- ## 2. Inheritance & Polymorphism (OOP) Keywords (`virtual`, `= 0`, `override`, `final`) These keywords govern runtime dynamic binding and overriding rules in inheritance hierarchies (see [Object-Oriented Programming]({{< relref "/programming/cpp/primer/oop.md" >}})). | Keyword / Specifier | Placement | Meaning & Core Role | | :--- | :--- | :--- | | **`virtual`** | Before a base-class non-static member function or destructor | **Enables runtime dynamic binding** when called through a base pointer (`Base*`) or reference (`Base&`), dispatching to the derived implementation matching the runtime Dynamic Type (without `virtual`, defining a same-named function in `Derived` merely causes **Name Hiding** rather than overriding, so calling through `Base*` always invokes `Base`'s version).
⚠️ **Polymorphic base classes must declare their destructor `virtual` (`virtual ~Base()`)** to prevent derived memory leaks on `delete basePtr;`. | | **`= 0`**
(Pure Virtual) | Immediately before the semicolon (`;`) of a `virtual` declaration | Declares a **Pure Virtual Function** with no required base implementation. Any class with one or more pure virtual functions becomes an **Abstract Base Class (ABC)** that cannot be instantiated directly. | | **`override`** | After the parameter list (and `const`) of a derived virtual function | Explicitly states that this function **overrides a base `virtual` function**. Emits an **immediate compile error** if the signature does not match the base virtual function. | | **`final`** | After a class name (`class D final`) or after a virtual function (`void f() override final;`) | On a class: **seals the class against any further inheritance**. On a virtual function: **prohibits further derived classes from overriding that function**. | ```cpp class Shape { // Abstract Base Class, cannot be instantiated directly public: virtual ~Shape() = default; // Essential virtual destructor virtual double area() const = 0; // '= 0': pure virtual function }; class Circle : public Shape { public: explicit Circle(double r) : radius_(r) {} double area() const override final { // 'final' on function: subclasses of Circle cannot override area()! return 3.1415926535 * radius_ * radius_; } private: double radius_; }; class UnitCircle final : public Circle { // 'final' on class: UnitCircle itself cannot be inherited from! public: UnitCircle() : Circle(1.0) {} // double area() const override; // Compile error! Circle::area() is marked final }; ``` --- ## 3. Constness, Optimization Control & Compile-Time Keywords (`const`, `volatile`, `constexpr`, `mutable`) These keywords control mutability, compiler optimization suppression, and compile-time constant evaluation (see [Variables and Basic Types]({{< relref "/programming/cpp/primer/variables-and-basic-types.md" >}}) and [Classes]({{< relref "/programming/cpp/primer/classes.md" >}})). | Keyword | Target | Meaning & Core Role | | :--- | :--- | :--- | | **`const`** | Variables, pointers/references, return types (`const T& f()`), after member function `()` (`void f() const`) | 1. **Variables/Pointers:** Prevents modification after initialization (see [Top-Level vs. Low-Level const]({{< relref "/programming/cpp/primer/variables-and-basic-types.md#top-level-vs-low-level-const" >}})).
2. **Function Return Type (`const T& f()`):** Written before the function name to qualify the return type, preventing callers from mutating the underlying data through a returned reference or pointer.
3. **Const Member Function (`void f() const`):** Written after `()` to make the implicit `this` pointer `const CLASS* const`, preventing member mutation and **allowing the function to be called on both non-`const` and `const` objects**. | | **`volatile`** | Variables, pointers/references (`volatile int* p`), after member function parameter list | 1. **Prevents Compiler Optimization:** Indicates that the memory can change asynchronously outside normal program flow (e.g., memory-mapped hardware registers or OS signal handlers), forcing every read/write to go directly to memory without register caching or elision (forms the **cv-qualifiers** alongside `const`).
2. **Warning (Not for Threading):** Unlike Java/C#, C++ `volatile` guarantees **neither atomicity nor CPU memory ordering (memory barriers)**. Always use `std::atomic` or a mutex for inter-thread communication. | | **`constexpr`** | Variables & functions (`constexpr int sq(int x)`) | Instructs the compiler to verify that a variable or function **can be evaluated at compile time as a constant expression**. | | **`mutable`** | Class data members or value-capturing lambdas (`[v]() mutable`) | 1. **Class Data Member:** Allows a member (e.g., access counter, cache, mutex) to be **modified even inside a `const` member function or on a `const` object**.
2. **Lambda Expression:** Allows modifying by-value captured copies inside the lambda body (which is `operator() const` by default). | ```cpp class Screen { public: void display() const { // const member function: ordinary members cannot be modified ++access_ctr_; // OK! mutable member can be modified even inside a const member function } private: mutable int access_ctr_ = 0; }; ``` --- ## 4. Type Deduction Keywords (`auto`, `decltype`) These keywords instruct the compiler to deduce types from expressions (see [Variables and Basic Types]({{< relref "/programming/cpp/primer/variables-and-basic-types.md" >}}) and [Expressions]({{< relref "/programming/cpp/primer/expressions.md" >}})). | Keyword | Deduction Basis | Core Deduction Rules | | :--- | :--- | :--- | | **`auto`** | Deduced from the **initializer value** | Strips **references (`&`) and top-level `const`** by default. Write **`const auto&`** explicitly when you want to bind by reference without copying or preserve top-level constness. | | **`decltype(expr)`** | Deduced from the **declared type or value category of `expr`** (without evaluating `expr`) | Preserves top-level `const` and references by returning the exact declared type for unparenthesized variable names, or a type matching the expression's value category (lvalue → `T&`, xvalue → `T&&`, prvalue → `T`) for all other expressions.
*(For the two-step deduction rules, see [Expressions: decltype]({{< relref "/programming/cpp/primer/expressions.md#decltype-two-step-deduction-rules-and-expressions" >}}))* | --- ## 5. Lifetime, Linkage & Encapsulation Keywords (`static`, `extern`, `inline`, `friend`) These keywords control object lifetime, cross-file linkage, inlining, and private member access (see [Functions]({{< relref "/programming/cpp/primer/functions.md" >}}) and [Classes]({{< relref "/programming/cpp/primer/classes.md" >}})). | Keyword | Context | Meaning & Core Role | | :--- | :--- | :--- | | **`static`** | **1. Local variable inside a function**
**2. Class data/function member**
**3. File-scope variable/function** | 1. **Local `static` object:** Initialized once on first execution and **persists until program termination**.
2. **Class `static` member:** Shared by all instances of the class rather than stored per object. `static` member functions have **no `this` pointer and cannot be declared `const`**.
3. **File-scope `static`:** Restricts visibility to the current translation unit (**Internal Linkage**; see [Namespaces]({{< relref "/programming/cpp/primer/namespaces.md#2-unnamed-anonymous-namespaces-and-internal-linkage" >}}) for a comparison with anonymous namespaces). | | **`extern`** | Variable declaration (`extern int x;`) | **Declares** a global variable or `extern const` constant defined in another translation unit without allocating storage (not a definition). | | **`inline`** | Function declarations/definitions & `constexpr` functions | Requests inline expansion at call sites to avoid call overhead, and **permits placing function definitions in header files (`.h`) without triggering multiple-definition linker errors (ODR violations)**. | | **`friend`** | Inside a class body (granting access to a nonmember function or class) | Grants a nonmember function (e.g., `operator<<`, `operator+`) or another class **direct access to `private` and `protected` members**. Friendship is **neither inherited nor transitive**. |