error instantiating redBlackTree template
Asked Answered
M

1

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.

Mingle answered 8/11, 2012 at 19:2 Comment(3)
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)Verdellverderer
@ratchetfreak that worked, thanks. Any idea why? Post your answer and I will mark it correct.Mingle
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.Listing
V
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)

Verdellverderer answered 8/11, 2012 at 19:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.