[Issue 6377] std.conv.to should check range when changing signedness

d-bugmail at puremagic.com d-bugmail at puremagic.com
Sun Jul 24 16:20:41 PDT 2011


http://d.puremagic.com/issues/show_bug.cgi?id=6377



--- Comment #3 from Jonathan M Davis <jmdavisProg at gmx.com> 2011-07-24 16:20:40 PDT ---
It depends on what you mean by "safe." When we talk about safe, we usually mean
"memory safe," and this conversion is perfectly safe as far as memory goes. And
even with regards to other types of safe, whether converting -1 to uint could
still be safe. There are plenty of cases where people do that _on purpose_.
Obviously, if you're looking to ensure that negative values aren't converted to
unsigned ones, this isn't safe. So, it depends entirely on what you're trying
to do. And generally speaking, safe refers to memory safety, and this _is_ safe
in that sense. I don't think that we ever actually use the term safe to refer
to making sure that a negative value isn't converted to an unsigned value or
that an unsigned value which is outside of the range of a signed value isn't
converted to a signed one.

Now, std.conv _will_ throw a ConvOverflowException error in cases where you do
something like convert an int to a byte when it won't fit, so there's a
definite argument that converting from -1 to uint with std.conv.to should
throw. But the language itself considers int and uint interchangeable as far as
conversions go. No cast is even necessary (while it _would_ be when converting
from int to byte).

So, whether to!uint(-1) should throw or not is up for debate. It's completely
memory safe, and the language itself has no problem with the conversion and
requires no cast. But there are plenty of people who would want it to catch
such an overflow just like it would when converting from an int to a byte.

So, this is a perfectly legitimate enhancement request, and we may very well
want to change how it acts in this case, but it's not a bug.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------


More information about the Digitalmars-d-bugs mailing list