Template specialisation for range of types

Jerry via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sun Mar 12 12:24:14 PDT 2017


On Sunday, 12 March 2017 at 18:49:22 UTC, data pulverizer wrote:
> 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

Wouldn't just putting const infront work?


More information about the Digitalmars-d-learn mailing list