Thread: reinterpret vs c-style cast

  1. #16
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by laserlight View Post
    I see that as a Good Thing. It makes you take more time to decide if you really need the cast, and if so, if it is the right cast to use.
    I fail to see that. I will reconsider it if it brings too much complexity or a big performance hit, but not if it's too long to spell. I will simply use the better alternative - C-style casts.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  2. #17
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Elysia
    I fail to see that. I will reconsider it if it brings too much complexity or a big performance hit, but not if it's too long to spell.
    The point is not about reconsidering, but about considering.

    Quote Originally Posted by Elysia
    I will simply use the better alternative - C-style casts.
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #18
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by laserlight View Post
    The point is not about reconsidering, but about considering.
    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!

    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.
    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.
    In short - C-style casts are better cause it is less to type.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #19
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Elysia View Post
    I fail to see that. I will reconsider it if it brings too much complexity or a big performance hit, but not if it's too long to spell. I will simply use the better alternative - C-style casts.
    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.

  5. #20
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Elysia
    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!
    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++."

    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.

    Quote Originally Posted by Elysia
    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.
    In short - C-style casts are better cause it is less to type.
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #21
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Not implying that C-style casts are more "efficient" than C++-style casts. I am merely pointing out that whenever I need to use casts, I would use C-style casts, even though they are "inferior" to C++-style casts, but because they are shorter.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #22
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by laserlight View Post
    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++."
    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.

    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 is actually a very good advice which I will be considering for the future.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #23
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Elysia
    I am merely pointing out that whenever I need to use casts, I would use C-style casts, even though they are "inferior" to C++-style casts, but because they are shorter.
    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.

    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?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #24
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by laserlight View Post
    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.
    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.

    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?
    I would not, but then again, I would name name something
    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.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #25
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Elysia
    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?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #26
    Kung Fu Kitty Angus's Avatar
    Join Date
    Oct 2008
    Location
    Montreal, Canada
    Posts
    115
    Quote Originally Posted by Elysia View Post
    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.
    Have you ever used the volatile keyword? The last time I used it, I had to use const_cast like crazy. If that was the wrong thing to do, then the right thing to do is very elusive.

  12. #27
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Angus
    The last time I used it, I had to use const_cast like crazy.
    Unfortunately, a C-style cast is coarse-grained enough to replace const_cast.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  13. #28
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by laserlight View Post
    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?
    For variable names, perhaps. I do prefer shorter, if possible, but for casts, I feel it is too long.

    Quote Originally Posted by Angus View Post
    Have you ever used the volatile keyword? The last time I used it, I had to use const_cast like crazy. If that was the wrong thing to do, then the right thing to do is very elusive.
    What does volatile have to do with const_cast?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  14. #29
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Elysia View Post
    What does volatile have to do with const_cast?
    Like const, volatile should not be possible to cast away with for example reinterpret_cast, so you use const_cast to remove (or add) volatile to a type.

    --
    Mats
    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.

  15. #30
    Kung Fu Kitty Angus's Avatar
    Join Date
    Oct 2008
    Location
    Montreal, Canada
    Posts
    115
    Quote Originally Posted by Elysia View Post
    What does volatile have to do with const_cast?
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reinterpret Cast to char* for Serialization?
    By 691175002 in forum C++ Programming
    Replies: 3
    Last Post: 02-01-2008, 07:44 PM
  2. Including The Right DLLs
    By bumfluff in forum Game Programming
    Replies: 8
    Last Post: 12-28-2006, 03:32 AM
  3. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  4. Converting Double to Float
    By thetinman in forum C++ Programming
    Replies: 7
    Last Post: 06-17-2006, 02:46 PM
  5. Tab Controls - API
    By -KEN- in forum Windows Programming
    Replies: 7
    Last Post: 06-02-2002, 09:44 AM