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





On 10 May 2017 at 20:15, Chris Vine <vine35792468 gmail com> wrote:

You can use reinterpret_cast when casting between C types which employ
"C style" inheritance (that is, where the "parent" is a struct which is
the first member of the "child" struct).  This is because C structs are
of necessity standard layout types in C++ speak.  More to the point,
because such inheritance is not inheritance in the C++ sense (they are
unrelated types from the C++ inheritance point of view) casting can
only be done using a C cast or a reinterpret_cast, which is guaranteed
to work by §9.2/20 of the C++ standard.

If that was what your post was about, then I think you are fine.

Thanks for the thoughts!

Yeah, the cases I am pondering at the moment are casts between GObjects, using the typical mode of inheritance that you described. static_cast<> doesn't work, but reinterpret_cast<> seems to. It sounds like this is a case for which reinterpret_cast<> does work, and not one of the perilous ones that you mentioned later.

Do you have any recommended reading on the cases which C casts can handle, and adjust as required, but reinterpret_cast<> cannot? That was surprising to me as I thought the C++ casts could cover all the angles.



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