[dmd-beta] D 2.059 beta 3

Jonathan M Davis jmdavisProg at gmx.com
Sun Apr 8 16:36:24 PDT 2012


On Saturday, April 07, 2012 22:15:53 Walter Bright wrote:
> http://ftp.digitalmars.com/dmd2beta.zip

With this release, it looks like opCmp (like opEquals) needs to have overloads 
which take both const ref and non-ref, which is fine. But the obvious way to 
implement this seems to result in infinite recursion:

import std.string;

struct S
{
    string s;

    int opCmp(S rhs) const
    {
        return opCmp(rhs);
    }

    int opCmp(const ref S rhs) const
    {
        return cmp(s, rhs.s);
    }
}

void main()
{
    assert(S("h") < S("w"));
    assert(S("w") > S("h"));

    auto h = S("h");
    auto w = S("w");

    assert(h < w);
    assert(w > h);
}


Shouldn't the non-ref opCmp being calling the ref one and not itself? Right 
now, it's resulting in a segfault, because it's calling itself. Is this a bug, 
or is it expected behavior? I'm inclined to say that it's a bug and a rather 
serious issue in light of needing to duplicate functions like opEquals and 
opCmp. But I may be missing something here.

- Jonathan M Davis


More information about the dmd-beta mailing list