`default` — C++ Keyword

`default` — C++ Keyword

The default keyword in C++: fallback branch in a switch statement, and default arguments/definitions.

How to use this reference page

Use reference pages to confirm names, categories, nearby facilities, and the constraints that matter before writing or reviewing code.

  • Scan the top of the page first to identify the primary types, functions, or algorithm families involved.
  • Use the nearby-page links when your question is really about a companion header, related algorithm family, or broader subsystem.
  • Validate tricky behavior with a small compileable example before relying on memory for details like invalidation, ordering, allocation, or lifetime rules.

default

In a switch statement, default marks the fallback branch executed when no case label matches. It also appears in function declarations (= default) to request a compiler-generated special member.

Syntax

// switch fallback
switch (expr) {
    case N: ...
    default: statements
}

// compiler-generated special member
ClassName() = default;

Example

#include <print>

int main() {
    int code = 99;

    switch (code) {
        case 0:  std::println("OK");      break;
        case 1:  std::println("Error");   break;
        default: std::println("Unknown"); break;  // executed
    }
}

struct Point {
    int x, y;
    Point() = default;   // compiler generates the default constructor
};

Notes

Example in practice

int main() {
    // Pick one facility from this reference page.
    // Write the smallest program that exercises its main precondition,
    // complexity rule, or lifetime constraint before scaling up.
    return 0;
}