1

This question is a little subjective, but I believe it may lead to some constructive answers. Assume I have char x; and I want to promote it to an integral so I can access it's numeric value. Which should I prefer to use and why? static_cast<int> or unary arithmetic operator +?

My own thoughts are that using a static_cast<int> is more explicit and readable. However, using the unary arithmetic operator + is shorter code.

Is one way safer than the other?

Wandering Fool
  • 2,170
  • 3
  • 18
  • 48
  • Just a note as it's easy to overlook: as `char` may be signed or unsigned, when you specifically want an `int` in the range 0..255 you need something like `static_cast(static_cast(c))`. – Tony Delroy May 26 '15 at 04:28
  • @TonyD Not following that last part. Don't you mean `static_cast(x)` or `static_cast(x)` for `x` is _unsigned char_ and _signed char_ respectively? – Wandering Fool May 26 '15 at 04:43
  • If the implementation's `char` type happens to be signed, then you start with a -128..127 value: if you cast that straight to `unsigned int` then negative values become "huge" positive values, while `static_cast` for a signed `char` still gives you -128..127 values, which is why you want to cast it to `unsigned char`: then you're dealing with a 0..255 value. The outer `static_cast(...)`: sorry, shouldn't have mentioned it in this context (I use it when streaming the ASCII value so I get the `operator<<(int)` overload giving e.g. "65" and not `operator<<(char)` e.g. 'A'). – Tony Delroy May 26 '15 at 05:58
  • @TonyD I'm following you a bit better now but I have one small confusion. What if you have a `signed char` that's negative, let's say -65. Would we still want to have a range from 0..255 for our `int` and if so, wouldn't static casting our `signed char` to an `unsigned char` also overflow giving us an undesired cast to `int`? – Wandering Fool May 26 '15 at 16:04
  • @TonyD It took me a while but I figured out the answer to my own confusion. For every negative char value there exists a positive char value from 128 - 255 inclusive. These positive char values share the same binary number as their negative counterparts. For example, -1 is 1111 1111 in binary and 255 is 1111 1111 in binary. This is because there is no way to know if a binary number is negative or positive unless you are told it is a signed value. In which case, you refer to the left most number. If it is a 1, it's negative. If it is a 0, it's positive. So in this case, your logic makes sense. – Wandering Fool May 27 '15 at 00:07
  • Because if I were to `static_cast(signed char)` and let's say our signed char is `-1`. Then it would be cast to `255` since they both have the same binary value. And so, we can safely cast it again to an integer and know that it's value will be from 0..255. – Wandering Fool May 27 '15 at 00:10
  • just seen your last three posts, and you have indeed worked it all out beautifully. Might be time for a new nickname ;-). Cheers. – Tony Delroy May 27 '15 at 04:51

1 Answers1

2

IMO, there is one important advantage of using static_cast, even if it is a little bit more verbose: it allows you to quickly search your code for casts via grep or any other text searching utility. In large projects this may make the difference, since otherwise C-style casts are often mistakenly matched in functions declarations such as void f(double), where (double) is of course not a cast.

Moreover, static_cast makes perfectly clear your intention. Using the + operator may seem exotic (incomprehensible) to someone who is not 100% familiar with integer promotions rules. In terms of "safety", afaik, both are equally good.

Related: What is the difference between static_cast<> and C style casting?

Community
  • 1
  • 1
vsoftco
  • 55,410
  • 12
  • 139
  • 252