On Monday, 24 November 2014 at 19:06:35 UTC, Matthias Bentrup wrote:
Agreed, though I don't like the explosion of new operators. I'd
prefer the C# syntax like check(<expression>), wrap(expression),
saturate(expression).

You maybe like this:
-------------------small test 1--------------------------
import std.stdio;

template  subuint(T1,T2){
auto subuint(T1 x, T2 y, ref bool overflow)
{
if(is(T1 == uint) && is(T2==uint))
        {
                if (x < y)
                {
                return cast(int)(x -y);
                }
        else
        {
                return x - y;
        }
        }
        else if(is(T1 == uint) && is(T2==int))
        {writeln("enter here1");

                if (x < y)
                { writeln("enter here2");
                        return cast(int)(x -y);
                }
                else
                { writeln("enter here3");
                        return x - y;
                }
        }
        else if(is(T1 == int) && is(T2==uint))
        {
                if (x < y)
                {
                return cast(int)(x -y);
                }
        else
        {
                return x - y;
        }
        }
        else if(is(T1 == int) && is(T2==int))
        {
                return x - y;
        }
  }
}

unittest
{
    bool overflow;
    assert(subuint(3, 2, overflow) == 1);
   assert(!overflow);
    assert(subuint(3, 4, overflow) == -1);

    assert(!overflow);
    assert(subuint(uint.max, 1, overflow) == uint.max - 1);
    writeln("typeid = ",typeid(subuint(uint.max, 1, overflow)));
    assert(!overflow);
    assert(subuint(1, 1, overflow) == uint.min);
    assert(!overflow);
    assert(subuint(0, 1, overflow) == -1);
    assert(!overflow);
    assert(subuint(uint.max - 1, uint.max, overflow) == -1);
    assert(!overflow);
    assert(subuint(0, 0, overflow) == 0);
    assert(!overflow);

        assert(subuint(3, -2, overflow) == 5);
    assert(!overflow);
    assert(subuint(uint.max, -1, overflow) == uint.max + 1);

    assert(!overflow);
    assert(subuint(1, -1, overflow) == 2);
    assert(!overflow);
    assert(subuint(0, -1, overflow) == 1);
    assert(!overflow);
    assert(subuint(uint.max - 1, int.max, overflow) == int.max);
    assert(!overflow);
    assert(subuint(0, 0, overflow) == 0);
    assert(!overflow);
    assert(subuint(-2, 1, overflow) == -3);
    assert(!overflow);
}


void main()
{
         uint a= 3;
         int b = 4;
         int c =2;
          writeln("c -a =",c-a);
         writeln("a -b =",a-b);
          writeln("----------------");
        bool overflow;
writeln("typeid = ",typeid(subuint(a, b, overflow)),", a-b=",subuint(a, b, overflow));
        writeln("ok");
}

---------------here is a simple ,but it's error--------------------------
import std.stdio;

template  subuint(T1,T2){
auto subuint(T1 x, T2 y, ref bool overflow)
{
        if(is(T1 == int) && is(T2==int))
        {
                return x - y;
        }
else if((is(T1 == uint) && is(T2==int)) | (is(T1 == uint) && is(T2==uint)) | (is(T1 == int) && is(T2==uint)))
        {
                if (x < y)
                {
                return cast(int)(x -y);
                }
        else
        {
                return x - y;
        }
        }
  }
}


void main()
{
         uint a= 3;
         int b = 4;
         int c =2;
          writeln("c -a =",c-a);
         writeln("a -b =",a-b);
          writeln("----------------");
        bool overflow;
writeln("typeid = ",typeid(subuint(a, b, overflow)),", a-b=",subuint(a, b, overflow));
        writeln("ok");
}

Reply via email to