Thread: string type troubles

  1. #31
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Quote Originally Posted by Elysia View Post
    C++ casts are too long to type.
    The long names help them stand out. They shouldn't be used frequently, so typing a long name shouldn't be much trouble.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  2. #32
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  3. #33
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by Elysia View Post
    C++ casts are too long to type.
    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.

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

  4. #34
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    C++ casts are too long to type.
    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 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

  5. #35
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by King Mir View Post
    Why? That link doesn't explain.
    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?

  6. #36
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    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?
    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 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

  7. #37
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by laserlight View Post
    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)).
    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.

  8. #38
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by King Mir View Post
    The long names help them stand out. They shouldn't be used frequently, so typing a long name shouldn't be much trouble.
    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.

    Quote Originally Posted by cpjust View Post
    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.

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

    Quote Originally Posted by laserlight View Post
    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++."
    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.
    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.

  9. #39
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Quote Originally Posted by Elysia View Post
    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.
    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.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ ini file reader problems
    By guitarist809 in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2008, 06:02 AM
  2. Can you check what is wrong with this code
    By Ron in forum C++ Programming
    Replies: 4
    Last Post: 08-01-2008, 10:59 PM
  3. Using VC Toolkit 2003
    By Noobwaker in forum Windows Programming
    Replies: 8
    Last Post: 03-13-2006, 07:33 AM
  4. Question on l-values.
    By Hulag in forum C++ Programming
    Replies: 6
    Last Post: 10-13-2005, 04:33 PM
  5. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM