Question about ubyte x overflow, any safe way?

Paul Backus snarwin at gmail.com
Sun Aug 4 18:38:34 UTC 2019


On Sunday, 4 August 2019 at 18:22:30 UTC, matheus wrote:
> On Sunday, 4 August 2019 at 18:15:30 UTC, Max Haughton wrote:
>> What do you want to do? If you just want to count to 255 then 
>> use a foreach
>
> This was just an example, what I'd like in this code is either: 
> Get an error (exception) when overflow or even an warning (Only 
> if "some" flag was active).

Use std.experimental.checkedint:

import std.stdio;
import std.experimental.checkedint;

void main()
{
     for(Checked!(ubyte, Throw) u = ubyte(250); u < 256; ++u) {
         writeln(u.get);
     }
}

An exception will be thrown when you attempt to increment u above 
255.


More information about the Digitalmars-d-learn mailing list