[Issue 9935] static if evaluation of template causes OOM/stack overflow.

d-bugmail at puremagic.com d-bugmail at puremagic.com
Mon Oct 7 00:10:10 PDT 2013


http://d.puremagic.com/issues/show_bug.cgi?id=9935


Walter Bright <bugzilla at digitalmars.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |bugzilla at digitalmars.com


--- Comment #4 from Walter Bright <bugzilla at digitalmars.com> 2013-10-07 00:10:09 PDT ---
The example code:
------------------
template TypeTuple(TList...)
{
    alias TList TypeTuple;
}

template staticIndexOf(T, TList...)
{
    enum staticIndexOf = genericIndexOf!(T, TList).index;
}

template genericIndexOf(args...)
{
    alias args[0]e;
    alias   args[1 .. $]  tuple;

    alias Alias!(tuple[0]) head;
    alias   tuple[1 .. $]  tail;

    static if (isSame!(e, head))
    {
        enum index = 0;
    }
    else
    {
        enum next  = genericIndexOf!(e, tail).index;
        enum index = (next == -1) ? -1 : 1 + next;
    }
}

template Replace(alias T, alias U, TList...)
{
    alias GenericReplace!(T, U, TList).result Replace;
}

template GenericReplace(args...)
{
    alias args[]from;
    alias args[1]to;
    alias   args[2 .. $]  tuple;

    alias Alias!(tuple[0]) head;
    alias    tuple[1 .. $] tail;

    static if (isSame!(from, head))
        alias TypeTuple!(to, tail) result;
    else
        alias TypeTuple!(head,
                         GenericReplace!(from, to, tail).result) result;
}

unittest
{
    static assert(Pack!(Replace!(1111, "11",
                                 2222, 1111, 1111, 1111)).
                         equals!(2222, "11", 1111, 1111));
}

template Alias(a...)
{
    alias a Alias;
}

template isSame(ab...)
{
    static if (!__traits(compiles, expectType!ab) &&
               __traits(compiles, expectBool!(ab[0] == ab[1])))
        static if (!__traits(compiles) )
        enum isSame = ab[0] == ab[1];
    else
        enum isSame = (isSame);
    else
        enum isSame = __traits(isSame, ab, ab);
}

template expectBool(bool b) {}

template Pack(T...)
{
    template equals(U...)
    {
        static if (T.length == 0)
            enum equals = true;
        else
            enum equals = isSame!(T, U) && Pack!(T[1 .. $]).equals!(U);
    }
}

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------


More information about the Digitalmars-d-bugs mailing list