CKTIS / C++
static_cast — explicit type conversion
static_cast<T>(expression) explicitly converts an expression to T under the language's conversion rules. The compiler checks whether the conversion is permitted, but does not guarantee value preservation or the validity of a class-hierarchy downcast.
Version: C++23; repository compiler flag, production runtime not attested
Syntax
static_cast<T>(expression)static_cast<T>(expression) requests an explicit conversion to T. It supports numeric conversions, enumeration conversions, and initialization using suitable explicit constructors or conversion functions, among other cases. static_cast cannot cast away const or volatile from a pointed-to or referenced type. A conversion to a value can, however, drop top-level qualifiers. For example, static_cast<int*>(p) is valid when p has type int* const: the resulting pointer value has no top-level const, and the pointed-to type is unchanged.
Casting to an lvalue reference produces an lvalue; casting to an rvalue reference to an object produces an xvalue; otherwise it produces a prvalue, except that an rvalue reference to a function produces an lvalue. Casting to void discards the expression's value, not its execution.
A permitted base-to-derived cast does not check the object's actual type at runtime. An incorrect object causes undefined behavior; casting a null pointer produces a null pointer. Downcasting through a virtual base is prohibited.
Floating-point conversion to int discards the fractional part toward zero; if the truncated value cannot fit in int, behavior is undefined. In the example, converting total before division prevents integer division. Casting the result of total / count afterward cannot recover the lost fractional part.
Examples
#include <iostream>
int main() {
const int total = 7;
const int count = 2;
const double ratio = static_cast<double>(total) / count;
const int truncated = static_cast<int>(-3.75);
std::cout << ratio << '\n' << truncated << '\n';
}
Result
3.5 -3
The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.