by jupp0r · 4y ago · 0 comments
I'm still wondering why `(int)` is spelled `reinterpret_cast<int>` in C++. Do they just like hitting keys on the keyboard?
1. because it is greppable and unsafe
2. because there is also static_cast and dynamic_cast
3. Because in C (int) is all three at once and not greppable -> C will let you do whatever, C++ will not let you do with a static_cast everything you can do with a (T) cast.
All in all, it is nice to ask, but if you do not know what you are talking about except for the surface, then, you should not say:
> Do they just like hitting keys on the keyboard?
No, we do not, but we hate even more to get an ungreppable, undecipherable (semantically) casting lost somewhere in several tens of thousands of lines of code :) This eases finding the suspicious code more easily.
And then I’d wrap that godawful cast in a function overloading the legacy C interface, so the overload has one job: to encapsulate the logic that the legacy function isn’t const correct. So then I’d have like void wrappedFoo(std::string_view s) { foo(const_cast<char*>(reinterpret_cast<const char*>(s.data())), s.size()); } with lots of comments about the cast.
But it is not unsafe, or rather, the C++ casts aren't more safe. They have the same semantics.
There are differences casting between class types, but with numeric types the issues are how to handle the value not fitting in (or being imprecise) in the destination type. Casting a value to int in C++ that overflows is still UB.
It doesn't fix C's other strange numeric issues either, like how `unsigned short` * `unsigned short` produces `int`.
There are C++-style casts that are compile-time errors that C would allow you to do in C style. For C everything is potentially a reinterpret cast basically.
C++ casts are safer than C's.
% echo 'int x = (int)1.2f;' > foo
% grep '(int)' foo
int x = (int)1.2f;
Does greppable mean something else in this context?I would grep 'reinterpret_cast' and I would set warning as error for the c-style casts. So you can assume my codebase does not have any.
I will find any cast to reference, pointer, etc. for any type. How would you do tjat if you do:
(int)a ((int)(MyType*))
and many, many more...
They are doing different things. For example, `reinterpret_cast<int>` cannot cast away constness but `(int)` can. There are other differences and you should never use C-style casts. In most C++ code bases I worked on, there were static code analysis checks pre merge that would prevent anybody from merging code with C-style casts to master for that reason.
Example: Variadic templates. When that happened, the problems we would bang our head against the wall with magically vanished. Twenty years ago, every implementation of the std library had serious bugs in it. It was avoided. Not the case today. I remember a std::map iterators not working. Watcom's C++ had serious flaws with it, but many exciting products were still created with it.
We didn't complain. We were happy, and worked around our problems.
I think the root of peoples issue with C++ is with template meta programming portions of it, which you can completely ignore, and grow into.
Almost all C++ programmers completely ignore it. You can too.
Like using C++ you mean?