Template specialisation for range of types
data pulverizer via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Sun Mar 12 13:45:35 PDT 2017
On Sunday, 12 March 2017 at 20:15:43 UTC, Meta wrote:
>
> import std.stdio : writeln;
> import std.traits : ConstOf;
>
> auto max(T)(T x, T y)
> {
> writeln("General template");
> return x > y ? x : y;
> }
>
>
> auto max(T: const U, U)(T* x, T* y) <----- Changed `ConstOf!U`
> to `const U`
> {
> writeln("Const template");
> return *x > *y ? x : y;
> }
>
>
> void main(){
> const double p = 2.4, q = 3;
> writeln(max(&p, &q)); //Prints "Const template"
> }
>
This is great Meta, thanks very much! I was trying to avoid using
template constraints because the more cases you add, the more
complicated the constraints get.
More information about the Digitalmars-d-learn
mailing list