CKTIS / C++
default — switch label and = default function definition
The default keyword has two distinct uses: it selects a path in switch when no case matches, or, in an = default definition, asks the compiler to generate a function according to language rules. It does not mean that data members are automatically zeroed.
Version: C++23; repository compiler flag, production runtime not attested
Syntax
switch (expression) { case constant: statements; break; default: statements; }
T() = default;
T(const T&) = default;
T& operator=(const T&) = default;
bool operator==(const T&) const = default;In switch, the default: label identifies where execution starts when no case matches. It is optional; one switch can have at most one such label. With no match and no default:, the switch body is skipped. Labels do not stop execution: execution can fall through from an earlier branch into default:. Use break to leave the switch.
An = default function definition replaces a handwritten body with an implementation specified by language rules. In C++23, it applies to special member functions—default, copy, and move constructors, copy and move assignment operators, and destructors—and to permitted comparison operators. It does not work for arbitrary functions; such a definition cannot have default arguments. = default; is not equivalent to an empty body {}.
A function with = default can be defined as deleted if required operations are unavailable. Defaulting on the first declaration does not make the function user-provided; defaulting it later does. Do not assume that members are zeroed: in the example, 7 comes from the initializer value = 7, not from = default itself.
The example demonstrates both uses. This entry targets C++23; the supplied standard URL points to an evolving draft that may contain later extensions. The compiler flag supports local verification, not confirmation of a production environment.
Examples
#include <iostream>
struct Counter {
int value = 7;
Counter() = default;
Counter(const Counter&) = default;
};
int main() {
Counter original;
Counter copy = original;
std::cout << copy.value << '\n';
switch (copy.value) {
case 0:
std::cout << "zero\n";
break;
default:
std::cout << "other\n";
break;
}
}
Result
7 other
The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.