[Issue 20478] New: Phobos should have a trait to identify NaN-like behavior

d-bugmail at puremagic.com d-bugmail at puremagic.com
Fri Jan 3 20:53:53 UTC 2020


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

          Issue ID: 20478
           Summary: Phobos should have a trait to identify NaN-like
                    behavior
           Product: D
           Version: D2
          Hardware: x86
                OS: Windows
            Status: NEW
          Severity: enhancement
          Priority: P1
         Component: phobos
          Assignee: nobody at puremagic.com
          Reporter: simen.kjaras at gmail.com

Since we have the pattern of returning float.nan from opCmp to signal
incomparable values, it makes sense to have a trait to identify this behavior.
Something like this:

template hasNanComparison(T1, T2 = T1)
{
    static if (isFloatingPoint!T1)
        enum hasNanComparison = true;
    else static if (!isAggregateType!T1)
        enum hasNanComparison = false;
    else static if (!is(typeof((T1 a, T2 b) => a.opCmp(b))))
        enum hasNanComparison = false;
    else
        enum hasNanComparison = hasNanComparison!(ReturnType!((T1 a, T2 b) =>
a.opCmp(b)));
}

///
@safe unittest
{
    assert( hasNanComparison!float);
    assert( hasNanComparison!real);
    assert(!hasNanComparison!int);
    assert(!hasNanComparison!string);
    assert(!hasNanComparison!(int*));
    assert(!hasNanComparison!(int[]));

    struct S
    {
        float opCmp(S s)
        {
            return float.nan;
        }
    }
    assert( hasNanComparison!S);

    struct S2
    {
        S opCmp(S2 s)
        {
            return S();
        }
    }
    assert( hasNanComparison!S2);
}

--


More information about the Digitalmars-d-bugs mailing list