Generic comparison

Paul Backus snarwin at gmail.com
Tue Nov 10 20:32:40 UTC 2020


On Tuesday, 10 November 2020 at 17:19:09 UTC, Ola Fosheim Grøstad 
wrote:
>
> Interesting, so "auto ref T" is the go-to type specifier for 
> generic code then?  I guess I also should conditionally add 
> things like pure, nogc, nothrow... I assume I would have to 
> test the comparison operator. I actually want to implement

The compiler infers pure, @nogc, nothrow etc. for template 
functions automatically. It's actually better if you don't add 
them by hand.

> (low <= value) && (value < high)
>
> So I guess I need to test both. But how...? compiles-trait?

You could add a template constraint, if you wanted. Something 
like:

     alias isOrderingComparableWith(T, U) = __traits(compiles, (T 
t, U u) => t < u);
     bool between(Value, Bound)(...)
         if (isOrderingComparaibleWith!(Value, Bound))

For a function this short, though, I don't think it's really 
necessary.


More information about the Digitalmars-d-learn mailing list