5

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.

YGL
  • 5,540
  • 2
  • 22
  • 19
  • the type needs to be comparable or you need to provide a comparator (apparently it doesn't consider `char` to be comparable try `dchar` instead) – ratchet freak Nov 08 '12 at 19:05
  • @ratchetfreak that worked, thanks. Any idea why? Post your answer and I will mark it correct. – YGL Nov 08 '12 at 19:09
  • 2
    Note: if you a looking for an 8 bit number use `byte` or `ubyte`, and if you need an associative container from that to `T` consider `T[256]` or `(T*)[256]`, it might be smaller and will be faster than a tree. – BCS Nov 09 '12 at 17:14

1 Answers1

6

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)

ratchet freak
  • 47,288
  • 5
  • 68
  • 106