Generic comparison

Ola Fosheim Grøstad ola.fosheim.grostad at gmail.com
Tue Nov 10 17:19:09 UTC 2020


On Tuesday, 10 November 2020 at 17:09:00 UTC, Paul Backus wrote:
> bool between(Value, Bound)(auto ref Value value, auto ref Bound 
> low, auto ref Bound high)
> {
>     return (low < value) && (value < high);
> }
>
> You need `auto ref` because either Bound or Value may have 
> copying disabled. Because the function is a template, 
> attributes like `scope` will be inferred when applicable 
> (modulo compiler bugs).

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

(low <= value) && (value < high)

So I guess I need to test both. But how...? compiles-trait?



More information about the Digitalmars-d-learn mailing list