[dmd-beta] D 2.059 beta 3

kenji hara k.hara.pg at gmail.com
Sun Apr 8 17:39:16 PDT 2012


2012年4月9日8:36 Jonathan M Davis <jmdavisProg at gmx.com>:
> 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
> _______________________________________________
> dmd-beta mailing list
> dmd-beta at puremagic.com
> http://lists.puremagic.com/mailman/listinfo/dmd-beta

I think this is an expected behavior.

struct S should have two opCmps, int opCmp(const ref S rhs) const and
int opCmp(const S rhs) const, both parameter rhs should be const.
Whole S is 'a type has mutable indirection' because S has a string
field s,  then copying const S to mutabe S is always forbidden.

>    int opCmp(S rhs) const
>    {
>        return opCmp(rhs);  // copy conversion from S to const S never run, so makes self-recursion.
>    }

I think the cause of issue 7864 is same.

Kenji Hara


More information about the dmd-beta mailing list