The point is not about reconsidering, but about considering.Originally Posted by Elysia
How are C-style casts the "better alternative" when they are too coarse grained, do not stand out and consequently are hard to find? Oh, and of course no C-style cast can replace dynamic_cast, if you really do need the functionality of dynamic_cast.Originally Posted by Elysia
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
The point is - I will never consider using a specific implementation or not because of the long names of casts. When I consider, I take everything else into consideration, but not the length of the casts!
Oh, don't get me wrong - it's great to have more safety in casts. If C++ casts were shorter, I would definitely use them.How are C-style casts the "better alternative" when they are too coarse grained, do not stand out and consequently are hard to find? Oh, and of course no C-style cast can replace dynamic_cast, if you really do need the functionality of dynamic_cast.
In short - C-style casts are better cause it is less to type.
There should be no performance difference between C style casts and C++ style casts. After all, the end result should be the same - the data converted from one type to another. When it comes to reinterpret cast it's SIMPLY telling the compiler that a pointer is a different type than the compiler would normally think the pointer to be - normal usage is when you store a pointer to an object in a void * - and there is no performance to be gained or lost in converting it from void * to Object * or Object * to void *, since the pointer is still the same value.
Edit: Obviously some casts do "cost", since they actually involve instructions to convert the data - float/double to integer or other way around for example, or lengthening/shortening data (e.g. short -> int or int -> short) - although they do not need in explicit cast. Still, there is no difference between C and C++ style casts here either.
--
Mats
Last edited by matsp; 11-19-2008 at 12:58 PM.
Read again: "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++."Originally Posted by Elysia
Clearly, you should not take the length of the cast operator into account when choosing to use the cast (or otherwise). The point is that, as a side effect of the name (and the fact that you have to pick one) and somewhat ugly syntactic form, you are more likely to even make a consideration instead of automatically reaching for a cast and moving on.
If the need to type less really bothers you, then define your own cast function templates as I have demonstrated... but I am not sure what names would be both short and descriptive.Originally Posted by Elysia
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
No, that is not the case. I would never even consider using a C++-style cast.
I do try to avoid all types of casts, though.
That is actually a very good advice which I will be considering for the future.If the need to type less really bothers you, then define your own cast function templates as I have demonstrated... but I am not sure what names would be both short and descriptive.
That sounds like a contradiction. You rightly point out that you should not make a decision about implementation "because of the long names of casts", then you now say that you choose C-style casts over C++ casts purely "because of the long names of casts", despite the advantages of C++ casts.Originally Posted by Elysia
Would you also choose to only use one or two character variable, function and class names because they are less to type than descriptive variable, function and class names?
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
I fail to see the contradiction.
I avoid casts if it can be done, because both C-style and C++-style casts "interfere" with the readability of code.
That mentioned, when I need casts, I use C-style, because C++-style are too long.
But the need to use casts aren't based on whether the casts are too long to type.
I would not, but then again, I would name name somethingWould you also choose to only use one or two character variable, function and class names because they are less to type than descriptive variable, function and class names?
a_function_or_variable_name_that_is_far_too_long
There is a limit to how much space we allocate for names, so to speak, and casts are no different.
I agree, but consider your own counterexample: granted, it is exaggerated, but is not even reinterpret_cast within the normal readability limits of such names?Originally Posted by Elysia
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
Unfortunately, a C-style cast is coarse-grained enough to replace const_cast.Originally Posted by Angus
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
I had made an object volatile in a multi-threaded app, and when I tried to call its members, the compiler gave me complaints about trying to pass it a volatile this pointer. So I needed to use const_cast to lift the volatile qualifier. But now Laserlight's got me wondering if there was any point in using the C++-style cast.