Comment on GotW #93 Solution: Auto Variables, Part 2 by Seungbeom Kim
> The first, again, is that int may not be big enough to avoid truncating the result, so we might lose information if x – y produces something larger than an int. Using auto can help with that.
My understanding is that, if x and y are int, then x - y is int and is not converted automatically to a bigger type if the result does not fit in an int, so overflow happens inside the expression x - y and auto doesn’t help here. For example, with int x = INT_MAX, y = INT_MIN;, auto diff = x - y; will be the same as int diff = x - y; and will overflow despite the auto; you should have promoted an operand (or both) to a bigger type before the subtraction operation. Is my understanding correct?
Read More »
No comments:
Post a Comment