Fwd: Replacing old C-style casts with static|reinterpret_cast?



On 10 May 2017 at 17:45, Nicklas Karlsson <nicklas karlsson17 gmail com> wrote:
> Murray mentioned on a Bugzilla that replacing these with static_cast<> is
> always good.

I think static_cast<> is very clear with what happens, data should not be changed just intepreted as the wanted type. There are cases then exactly this is needed and then it is really good.


There's no debate about static_cast, only reinterpret_cast.

That said, it's not true that static_cast only causes reinterpretation as the desired type: it can also invoke a conversion operator, which can then return absolutely anything.

static_cast only ensures that the compiler can find a previously defined route to perform the conversion, be it a relation by basic type class or inheritance, a conversion operator, or probably various other things I can't think of right now.

In fact, due to it being satisfied by an inheritance relationship, you can e.g. cast a Gtk::Widget* that really points at a Gtk::Label to a Gtk::Frame, and it won't complain, because it assumes you typed a sensible thing: it only checks the definedness of the conversion, not the potential danger level.




[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]