Hacker News new | past | comments | ask | show | jobs | submit login

For ord he could use std::to_string

    string ord(string const &s) {
      return to_string(unsigned(s[0]));
    }
He skips all the const-refs...



Yes, to_string would have been better

The C-style cast is shorter, but less safe, so you should not use it: http://stackoverflow.com/questions/1609163/what-is-the-diffe...

Yeah, I couldn't quite decide if I should include const refs. On the one hand, it is idiomatic, but on the other hand I didn't want to clutter the code with something that is just an optimization on paper.


I know the C-style casts are bad (although this is more of a C++-style-C-style cast. (unsigned) would have been the C-cast. But still bad). But in this situation it should be safe and for use in an example more readable.

btw. Please fix either the size of your code or the margins of your blog. You use only half the screen width but I still have to scroll sideways in the code listing.


How can a C-style cast from char to unsigned possibly be "less safe", and how could a different style of cast be more safe?


In this case it obviously doesn't matter, but in the general case it is better to be explicit on which type of cast you are doing. That way the compiler can statically stop you from doing stupid things. Read the link I gave.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: