Avatar ·

Why should you use static_cast<T>(x) instead of (T)x or T(x) in C++?

Hi everyone! I recently started learning C++ and noticed that in some textbooks and forums they recommend using `static_cast(x)` instead of the old C-style cast `(int)x` or the functional form `int(x)`. Honestly, this seems like unnecessary complexity to me. After all, `(int)x` is shorter and clearer, especially when you just need to convert a double to an int or something similar. Here's an example I often write: ```cpp double pi = 3.14159; int truncated = (int)pi; // or int truncated = int(pi); ``` Why should I write `static_cast(pi)` if the result is the same? They say static_cast is safer, but I don't understand the difference. Doesn't the C-style cast do the same thing? Or are there some pitfalls I don't know about? Maybe there are cases where `(