Need way to compare classes, and primitive types in bst Template
ag0aep6g via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Thu Jun 8 22:53:11 PDT 2017
On 06/09/2017 05:32 AM, Mark wrote:
> https://dpaste.dzfl.pl/ff58876ce213
[...]
> What Id like to do is this:
>
> auto tree = new BSTbase!int;
> ...
> tree.insert(7);
>
> and
>
> auto Tree2 = new BSTbase!Aclass;
> ...
> Tree2.insert(Aclassobject);
>
> What I have is:
> Tree.insert(7, cast(real) 7);
> and
> Tree2.insert(Aclassobject, cast(real) (cast(void*) Aclassobject));
>
> I tried a few things, but I can't get them to work.
>
> Can anyone give a way for me to avoid doing it this clumsy way?
Get rid of `real val;` and just compare `payload`s. For classes, you can
detect them with `static if (is(T == class))` or some such, and cast to
void* when comparing.
But when you cast to void*, you're ignoring an opEquals or opCmp the
class might have. That might be surprising. So maybe just require that
classes implement opCmp. Or try to detect when a class doesn't have
opCmp and only cast then.
More information about the Digitalmars-d-learn
mailing list