error instantiating redBlackTree template

做~自己de王妃 提交于 2019-12-10 14:47:35

问题


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)



来源:https://stackoverflow.com/questions/13295912/error-instantiating-redblacktree-template

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!