www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 7864] New: Structs with class members can't be sorted

reply d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=7864

           Summary: Structs with class members can't be sorted
           Product: D
           Version: unspecified
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: regression
          Priority: P2
         Component: DMD
        AssignedTo: nobody puremagic.com
        ReportedBy: jmdavisProg gmx.com



PDT ---
This compiles with 2.058, but not with the latest head (which I assume is the
same as 2.059 beta)

import std.algorithm;
import std.string;

class C
{
    string str;

    this(string str)
    {
        this.str = str;
    }

    override int opCmp(Object o) const
    {
        auto rhs = cast(C)o;
        assert(rhs);
        return cmp(str, rhs.str);
    }
}

struct S
{
    C c;

    this(C c)
    {
        this.c = c;
    }

    int opCmp(S rhs) const
    {
        if(c < rhs.c)
            return -1;
        if(c > rhs.c)
            return 1;
        return 0;
    }

    int opCmp(const ref S rhs) const
    {
        if(c < rhs.c)
            return -1;
        if(c > rhs.c)
            return 1;
        return 0;
    }
}

void main()
{
    S[] stuff;
    sort(stuff);
}


/home/jmdavis/dmd2/linux/bin/../../src/phobos/std/algorithm.d(6802): Error:
static assert  "Invalid predicate passed to sort: a < b"
q.d(52):        instantiated from here: sort!("a < b",cast(SwapStrategy)0,S[])



The same occurs if you use a SysTime instead of C, so the problem occurs
whether the class is directly or indirectly insed of S. This strikes me as

and this isn't.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Apr 08 2012
next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=7864


Kenji Hara <k.hara.pg gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
         Resolution|                            |INVALID




 This compiles with 2.058, but not with the latest head (which I assume is the
 same as 2.059 beta)
 
[snip]
 
 /home/jmdavis/dmd2/linux/bin/../../src/phobos/std/algorithm.d(6802): Error:
 static assert  "Invalid predicate passed to sort: a < b"
 q.d(52):        instantiated from here: sort!("a < b",cast(SwapStrategy)0,S[])
This is wrong opCmp overload problem, not compiler regression. If you change opCmp signatures to: int opCmp(const S rhs) const int opCmp(const ref S rhs) const // may work in general or: int opCmp(const S rhs) const // may work in general if S allows lvalue copying or: int opCmp(S rhs) const // In this case, sorted elements is always mutable so it works or: int opCmp(const ref S rhs) const // In this case, current std.algorithm.sort implementation only requires // lvalue comparison, so it works) Then you can compile it.
 The same occurs if you use a SysTime instead of C, so the problem occurs
 whether the class is directly or indirectly insed of S. This strikes me as

 and this isn't.
Bug 7808 was alias this type resolution problem. There is no matter. -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Apr 08 2012
prev sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=7864


bearophile_hugs eml.cc changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |bearophile_hugs eml.cc




 If you change opCmp signatures to:
 
     int opCmp(const S rhs) const
     int opCmp(const ref S rhs) const
     // may work in general
Maybe one opCmp is enough in general: struct S { C c; int opCmp()(const auto ref S rhs) const { if (c < rhs.c) return -1; if (c > rhs.c) return 1; return 0; } } -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Apr 09 2012