The static_cast operator converts a given expression to a specified type.
With the right angle bracket feature, you may specify a template_id as Type in the static_cast operator with the >> token in place of two consecutive > tokens. For details, see Class templates (C++ only).
An example of the static_cast operator:
#include <iostream>
using namespace std;
int main() {
int j = 41;
int v = 4;
float m = j/v;
float d = static_cast<float>(j)/v;
cout << "m = " << m << endl;
cout << "d = " << d << endl;
}
m = 10
d = 10.25
In this example, m = j/v; produces an answer of type int because both j and v are integers. Conversely, d = static_cast<float>(j)/v; produces an answer of type float. The static_cast operator converts variable j to type float. This allows the compiler to generate a division with an answer of type float. All static_cast operators resolve at compile time and do not remove any const or volatile modifiers.
Applying the static_cast operator to a null pointer converts it to a null pointer value of the target type.
You can explicitly convert a pointer to cv1 void to a pointer to cv2 void if cv2 is the same or more cv-qualification than cv1.