Possible Duplicate:
Regular cast vs. static_cast vs. dynamic_cast
In C++, why use static_cast<int>(x) instead of (int)x?
What is the difference between static_cast<float>(foo)
and float(foo)
? I often see static_cast<float>
or similar in template code to get from any integral type to some specific integral type. For example say I wanted a function to perform floating point division on any integral type. I normally see something like this:
template <typename T>
float float_div(T lhs, T rhs)
{
// I normally see this
return static_cast<float>(lhs) / static_cast<float>(rhs);
// But I could do this
// return float(lhs) / float(rhs); // But I could do this
}
Is there any advantage to using float(lhs)
or static_cast<float>(lhs)
? Also what is the name of the float(lhs)
initialization/conversion?