Hacker News new | ask | show | jobs
by zahlman 2 days ago
Many years ago I remember going through the Boost library and seeing C-style casts that seemed entirely gratuitous. I tried replacing them with what I was pretty sure were the equivalent C++ reinterpret_casts, and the result didn't compile. I never did figure it out.
2 comments

C style cast can be either a static_cast or reinterpret_cast, but it can also be a const_cast or a static+const or reinterpret+const. Finally, it will perform a static_cast that bypasses private inheritance (because the alternative would be to fall back to a reinterpret_cast, which is wrong if the static_cast needs to apply an offset to the pointer)
> a static_cast that bypasses private inheritance (because the alternative would be to fall back to a reinterpret_cast, which is wrong if the static_cast needs to apply an offset to the pointer)

That may well have been it, then. I would think that if it could have been expressed naturally, it would have been.

(I don't think I ever used private inheritance in my own C++ code. I'm not a huge fan of inheritance at all, so.)

To add on top of sibling comment, C style casts are too lose, and the main reason for the new C++ style casts is improved type safety.

So instead of anything goes, there is some additional type checking depending on the type of cast being made.