error instantiating redBlackTree template

2019-06-21 06:16发布

问题:

I'm having trouble instantiating a RedBlackTree container with chars, but it works with ints:

import std.stdio;
import std.container;

void main()
{
        auto r1 = redBlackTree!(int)();   // works
        auto r2 = redBlackTree!(char)();  // error instantiating
}

I'm using DMD32 D Compiler v2.060.

Any thoughts? Thanks.

回答1:

you need to use a type that is comparable (i.e. can use the < operator or provide your own comparator as the second template parameter

char (and wchar) is only useful for use in arrays due to one char not necessarily relating to an actual letter in unicode (UTF8 edition) this has other gotcha that will trip up new coders in D

dchar on the other hand will always correspond to a letter and as such is comparable to another letter,

rule of thumb in D always use dchar unless it's for a string type (and even then consider using dstring)