How can I save a class use the RedBlackTree?

Steven Schveighoffer via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue May 3 02:37:13 PDT 2016


On 5/3/16 5:10 AM, Dsby wrote:
> this is the test Code:
>
>
> import std.container.rbtree;
> import std.stdio;
>
> class TClass
> {
>      int i;
> }
>
> void main()
> {
>      RedBlackTree!(TClass) list = new RedBlackTree!(TClass)();
>      auto t = new TClass();
>      list.insert(t);
>      writeln("The rbtree length is ",list.length());
>      list.removeKey(t);
>      writeln("The rbtree length is ",list.length());
> }
>
> and thisis erro :
> /usr/include/dlang/dmd-2.071.0/phobos/std/functional.d-mixin-206(206):
> Error: mutable method object.Object.opCmp is not callable using a inout
> object
> /usr/include/dlang/dmd-2.071.0/phobos/std/container/rbtree.d(871):
> Error: template instance std.functional.binaryFun!("a < b", "a",
> "b").binaryFun!(inout(TClass), TClass) error instantiating
> rbtree.d(11):        instantiated from here: RedBlackTree!(TClass, "a <
> b", false)
> /usr/include/dlang/dmd-2.071.0/phobos/std/functional.d-mixin-206(206):
> Error: function object.Object.opCmp (Object o) is not callable using
> argument types (inout(TClass))
> /usr/include/dlang/dmd-2.071.0/phobos/std/container/rbtree.d(873):
> Error: template instance std.functional.binaryFun!("a < b", "a",
> "b").binaryFun!(TClass, inout(TClass)) error instantiating
> rbtree.d(11):        instantiated from here: RedBlackTree!(TClass, "a <
> b", false)
>


bleh, Object.opCmp only works with mutable objects. Which means 
RedBlackTree cannot work with objects because it uses inout to ensure 
your objects are not modified (since 2.068)

It doesn't work with const or immutable either, the only possible 
solution is to go back to mutable only.

So annoying, we need to fix Object.opCmp, or using const/immutable 
comparisons with objects doesn't work.

-Steve


More information about the Digitalmars-d-learn mailing list