Arrays

Koroskin Denis 2korden at gmail.com
Sat Mar 29 06:34:58 PDT 2008


On Sat, 29 Mar 2008 06:25:04 +0300, Walter Bright  
<newshound1 at digitalmars.com> wrote:

> Edward Diener wrote:
>> You can answer that question within any context without having to skew  
>> your argument against C++, and if the answer is that their is no  
>> advantage in using D over C++ it does not negate the areas where there  
>> may be advantages.
>
> What should I say to someone who says something like: "D is unusable  
> because it offers no protection against integer overflows, so I'll stick  
> with C++" ?

You said, no protection against integer overflows in D? You are wrong!

import std.stdio;
import std.traits;

Integral checked(Integral)(lazy Integral dg)
{
     static assert(isIntegral!(Integral));

     Integral result = dg();
     asm {
         jo overflow;
     }
     return result;

     overflow:
     throw new Exception("Integer overflow occured");
}

int main()
{
     int t = int.max;
     try
     {
         int s = checked(t + 1);
         writefln("Result is %d", s);
     }
     catch(Exception e)
     {
         writefln("Whoops! %s", e.toString());
     }
     return 0;
}

Permission to add this to Phobos is granted!



More information about the Digitalmars-d mailing list