static_cast explicitly converts a value to another type. The target type is written in angle brackets and the value in parentheses:
double d = 3.99;
int i = static_cast<int>(d); // 3static_cast satisfies brace initialisation, which would otherwise reject narrowing conversions, since the intent is explicit:
int i{static_cast<int>(3.99)};Conversions that make no sense are rejected at compile time:
int* p = static_cast<int*>(3.14); // ERROR.Warning
Avoid C-style casts, which silently try static_cast, const_cast, and reinterpret_cast in turn, so they can compile code that should not compile:
int i = (int)d; // Avoid.
int i = int(d); // Avoid.
Dan