Casts

The cast<T>(value) expression performs an explicit numeric conversion to a selected destination type.

Integer conversions

Use a cast when changing integer width or signedness. The destination type controls truncation, extension and signed interpretation.

long large = 1000;
int compact = cast<int>(large);

uint unsignedValue = 42;
int signedValue = cast<int>(unsignedValue);

Integer and floating-point conversions

Casts convert numeric values rather than reinterpreting their underlying bit pattern.

float ratio = 12.75f;
int whole = cast<int>(ratio);

int count = 8;
double precise = cast<double>(count);
Explicit intent

A cast makes width, signedness and numeric-domain changes visible at the point where they occur.