[Issue 13929] New: nothrow @nogc gcd with signed integers

via Digitalmars-d-bugs digitalmars-d-bugs at puremagic.com
Sat Jan 3 04:16:00 PST 2015


https://issues.dlang.org/show_bug.cgi?id=13929

          Issue ID: 13929
           Summary: nothrow @nogc gcd with signed integers
           Product: D
           Version: D2
          Hardware: x86
                OS: Windows
            Status: NEW
          Severity: enhancement
          Priority: P1
         Component: Phobos
          Assignee: nobody at puremagic.com
          Reporter: bearophile_hugs at eml.cc

void main() nothrow @nogc {
    import std.numeric: gcd;
    auto z = gcd(10, 20);
}


dmd 2.067alpha gives:

test.d(3,17): Error: @nogc function 'D main' cannot call non- at nogc function
'std.numeric.gcd!int.gcd'
test.d(3,17): Error: 'std.numeric.gcd!int.gcd' is not nothrow
test.d(1,6): Error: function 'D main' is nothrow yet may throw


Perhaps it's better to replace the enforce() with an assert in a pre-condition.

Currently gcd is implemented like this:


T gcd(T)(T a, T b)
{
    static if (is(T == const) || is(T == immutable))
    {
        return gcd!(Unqual!T)(a, b);
    }
    else
    {
        static if (T.min < 0)
        {
            enforce(a >= 0 && b >=0);
        }
        while (b)
        {
            auto t = b;
            b = a % b;
            a = t;
        }
        return a;
    }
}

--


More information about the Digitalmars-d-bugs mailing list