www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 18302] New: Add std.traits.Noqual

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

          Issue ID: 18302
           Summary: Add std.traits.Noqual
           Product: D
           Version: D2
          Hardware: x86
                OS: Windows
            Status: NEW
          Severity: enhancement
          Priority: P1
         Component: phobos
          Assignee: nobody puremagic.com
          Reporter: simen.kjaras gmail.com

As pointed out in bug 8338, Unqual only removes top-level qualifiers, leaving
Unqual!(const(int*)) as const(int)*. A desire has been expressed for a template
that does what Unqual does, but recursively applying itself to element- and
pointed-to types. POC:

template Noqual(T, size_t levels = size_t.max)
{
    static if (levels == 0)
    {
        alias Noqual = T;
    }
    else static if (is(T == U[], U))
    {
        alias Noqual = Noqual!(U, levels-1)[];
    }
    else static if (is(T == U[N], U, size_t N))
    {
        alias Noqual = Noqual!(U, levels-1)[N];
    }
    else static if (is(T == U[K], U, K))
    {
        alias Noqual = Noqual!(U, levels-1)[Noqual!(K, levels-1)];
    }
    else static if (is(T == U*, U))
    {
        alias Noqual = Noqual!(U, levels-1)*;
    }
    else
    {
        alias Noqual = Unqual!T;
    }
}

unittest
{
    assert(is(Noqual!(const(int*)) == int*));
    assert(is(Noqual!(const(int****)) == int****));
    assert(is(Noqual!(const(int****), 2) == const(int**)**));
    assert(is(Noqual!(const(int[int])) == int[int]));
    assert(is(Noqual!(const(int[int])*) == int[int]*));
    assert(is(Noqual!(const(int[const(int*)])*, 2) ==
const(int)[const(int)*]*));
    assert(is(Noqual!(const(int[3])) == int[3]));
}

--
Jan 26 2018