Template specialisation for range of types
data pulverizer via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Sun Mar 12 11:49:22 PDT 2017
Hello all,
I am attempting to write templates for differently qualified
types using specialisations. Below is an example for const and
non-const outlining my approach:
``````````````````````````
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: ConstOf!U, U)(T* x, T* y)
{
writeln("Const template");
return *x > *y ? x : y;
}
void main(){
const double p = 2.4, q = 3;
writeln(max(&p, &q));
}
``````````````````````````
I get this output:
General template
7FFE5B3759A8
In this case would like to use the ConstOf specialisation instead
of the default implementation for the inputs which are const.
Thanks for you answers in advance
More information about the Digitalmars-d-learn
mailing list