I\'ve heard that the static_cast
function should be preferred to C-style or simple function-style casting. Is this true? Why?
It's about how much type-safety you want to impose.
When you write (bar) foo
(which is equivalent to reinterpret_cast
if you haven't provided a type conversion operator) you are telling the compiler to ignore type safety, and just do as it's told.
When you write static_cast
you are asking the compiler to at least check that the type conversion makes sense and, for integral types, to insert some conversion code.
EDIT 2014-02-26
I wrote this answer more than 5 years ago, and I got it wrong. (See comments.) But it still gets upvotes!