Disable implicit conversion
Stewart Gordon
smjg_1998 at yahoo.com
Mon Mar 7 09:41:02 PST 2011
On 07/03/2011 16:40, Jesse Phillips wrote:
> KennyTM~ Wrote:
>
>> On Mar 7, 11 18:33, Eugene wrote:
>>> Hi!
>>>
>>> What I want to do is pretty simple. I need to subtract a ubyte from a
>>> ubyte and store the result in a ubyte. The problem is that DMD
>>> implicitly wants to convert the ubytes into an integer, so it fails
>>> when attempting to store the result into a ubyte. (Int cannot be
>>> converted to ubyte?)
I've been bitten by that annoyance while doing Java stuff in the past.
http://d.puremagic.com/issues/show_bug.cgi?id=1977
<snip>
> Not only that but the operation isn't any safer than a cast, if overflow didn't matter
> and you just need it to fit then this would be good.
>
> Lastly overflow does matter and if it happens the code will continue to silently work.
> I recommend using std.conv.to because it will throw an exception if an overflow were to
> happen.
If overflow of a ubyte matters, surely so does overflow of an int or long?
In the OP's case, the only possible overflow is a negative result. As such, if overflow
matters, one will do something like
if (u2 < u1) {
// ...
} else {
u3 = cast(ubyte) (u2 - u1);
}
For such simple cases as this, using a library function to convert an overflow into an
exception might be overkill.
Stewart.
More information about the Digitalmars-d
mailing list