Thread overview | |||||
---|---|---|---|---|---|
|
April 09, 2012 [Issue 7864] New: Structs with class members can't be sorted | ||||
---|---|---|---|---|
| ||||
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 --- Comment #0 from Jonathan M Davis <jmdavisProg@gmx.com> 2012-04-08 17:15:12 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 being similar to issue# 7808 (though not identical). That was fixed however, and this isn't. -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: ------- |
April 09, 2012 [Issue 7864] Structs with class members can't be sorted | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jonathan M Davis | 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 --- Comment #1 from Kenji Hara <k.hara.pg@gmail.com> 2012-04-08 19:17:46 PDT --- (In reply to comment #0) > 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 being similar to issue# 7808 (though not identical). That was fixed however, 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: ------- |
April 09, 2012 [Issue 7864] Structs with class members can't be sorted | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jonathan M Davis | http://d.puremagic.com/issues/show_bug.cgi?id=7864 bearophile_hugs@eml.cc changed: What |Removed |Added ---------------------------------------------------------------------------- CC| |bearophile_hugs@eml.cc --- Comment #2 from bearophile_hugs@eml.cc 2012-04-09 10:42:47 PDT --- (In reply to comment #1) > 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: ------- |
Copyright © 1999-2021 by the D Language Foundation