A predicate with different argument types

psycha0s box at mail.com
Sun Feb 10 17:10:47 UTC 2019


Is it possible to make a comparison predicate with different 
argument types? For instance, suppose I have a struct like this:

struct Attribute {
     string name;
     variant value;
}

Also, suppose I have a sorted vector of such values. So I can 
quickly find one by its name field and I have index-based random 
access to elements at the same time. Therefore I have something 
like this:

Attribute[] attributes;

Now, imagine I try to find an insertion position for a new 
element:

--------
string name; // input data

auto sorted = object_.assumeSorted!((a, b) => a.name < b.name);
Attribute dummy;
dummy.name = name;
auto pos = sorted.lowerBound(dummy);
--------

It works fine but is there a way to get rid of the 'dummy' 
object? I can easily achieve that in C++ since STL allows to have 
a predicate with different argument types. Ideally, I'd like 
something like this:

--------
string name; // input data

auto sorted = object_.assumeSorted!((a, b) => a.name < name);
auto pos = sorted.lowerBound(name);
--------

Thanks.


More information about the Digitalmars-d-learn mailing list