The long names help them stand out. They shouldn't be used frequently, so typing a long name shouldn't be much trouble.
Printable View
Typing std::lexicographical_compare_3way is too long to type too, but that doesn't mean I'm going to avoid using it if I need it. :p
C casts are dangerous!
You can't easily search for all C casts, unless you search specifically for the type like (int*) (char*) (float*)...
You can't tell why the cast was needed with a C cast. Did they need to remove const-ness, did they need to convert between related types, did they need to cast between base and derived classes...?
Like what King Mir pointed out, Stroustrup considers that a side benefit: "Maybe, because static_cast is so ugly and so relatively hard to type, you're more likely to think twice before using one? That would be good, because casts really are mostly avoidable in modern C++."Quote:
C++ casts are too long to type.
The hard copy version of the book does not provide any elaboration either, but I believe that Sutter and Alexandrescu are concerned because "the mapping performed by reinterpret_cast is implementation-defined", and thus "it might, or might not, produce a representation different from the original value" (section 5.2.10 of the C++ Standard (2003)), whereas for static_cast, 'a value of type pointer to object converted to "pointer to cv void" and back to the original pointer type will have its original value' (section 5.2.9 of the C++ Standard (2003)).Quote:
I don't think I fully understand it either; I just remember reading it in that book.
If anyone else here can explain what kind of danger the book is talking about, can you please enlighten us?
I was able to gather that much from the book, but what I'm not too sure about is how some types get messed up by reinterpret_cast so that they aren't equal to the original value if cast back again? An example of such a case and an explaination of what's happening would be helpful.
The problem is that I disagree with Bjarne's analogy "casts shouldn't be used so often." So I don't like go typing reinterpret_cast<char*>(something) everytime I need to cast something. I don't care what type it is I'm casting to or why.
Yes, I know C casts are too dangerous (I do remember), but IMO, they brought this upon themselves by making C++ casts long and tedious.
As for me, I don't work in projects with multiple people so I haven't had any problems searching for casts or wondering why they're there.
I'm experienced with types and I know exactly what they do - so using C casts have never really been a problem for me.
It also makes it a pain where you NEED to do it. Contrary to Bjarne, I do believe casts are more necessary than he admits and therefore I tend to use C-style casts, but use C++-style casts in examples.
I typically do use const_cast to get rid of const whenever necessary, too.
C++ is meant to be a type-safe language. What is it that you are doing that requires you to do away with this safety? Chances are if you find yourself having to use casts frequently, that there is something wrong with your approach.
The also long cast names serve as a yellow warning flag that something dangerous is going on.