[dmd-beta] D 2.059 beta 3
Nick Sabalausky
bus_dmdbeta at semitwist.com
Sun Apr 8 22:50:43 PDT 2012
From: "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.
>
I ran into that problem too. I ended up having to have the two opCmp's, and
make them both forward to an opCmp_. And then repeat the whole pattern for
opEquals and at least one other thing. One hell of a mess just to use basic
operator overloading.
More information about the dmd-beta
mailing list