Jump to page: 1 2
Thread overview
Template specialisation for range of types
Mar 12, 2017
data pulverizer
Mar 12, 2017
Jerry
Mar 12, 2017
ketmar
Mar 12, 2017
data pulverizer
Mar 12, 2017
ketmar
Mar 12, 2017
ketmar
Mar 12, 2017
Meta
Mar 12, 2017
ketmar
Mar 12, 2017
Meta
Mar 12, 2017
ketmar
Mar 12, 2017
data pulverizer
Mar 12, 2017
data pulverizer
Mar 13, 2017
Meta
March 12, 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

March 12, 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?
March 12, 2017
data pulverizer wrote:

> In this case would like to use the ConstOf specialisation instead of the default implementation for the inputs which are const.

actually, second template is uninstantiable at all. you want to do type deconstruction at instantiation, and that doesn't work.

i.e. what your code wants to do (as it is written) is to have `T` in second template to be equal to `double`. you cannot deconstruct the type like that in template. what you *can* do, though, is this:

 auto max(T)(const(T)* x, const(T)* y)

this way it will select your second template.
March 12, 2017
On Sunday, 12 March 2017 at 19:32:37 UTC, ketmar wrote:
> data pulverizer wrote:
>
>> In this case would like to use the ConstOf specialisation instead of the default implementation for the inputs which are const.
>
> actually, second template is uninstantiable at all. you want to do type deconstruction at instantiation, and that doesn't work.
>
> i.e. what your code wants to do (as it is written) is to have `T` in second template to be equal to `double`. you cannot deconstruct the type like that in template. what you *can* do, though, is this:
>
>  auto max(T)(const(T)* x, const(T)* y)
>
> this way it will select your second template.

If I change the implementation of the second template to your above declaration, I get the error:

max.max called with argument types (const(double)*, const(double)*) matches both:
max.d(34):     max.max!(const(double)*).max(const(double)* x, const(double)* y)
and:
max.d(42):     max.max!double.max(const(double)* x, const(double)* y)

I need at least those two implementation for the different cases, a general "default", and for specified types and type qualifications.

March 12, 2017
data pulverizer wrote:

> If I change the implementation of the second template to your above declaration, I get the error:
>
> max.max called with argument types (const(double)*, const(double)*) matches both:
> max.d(34):     max.max!(const(double)*).max(const(double)* x, const(double)* y)
> and:
> max.d(42):     max.max!double.max(const(double)* x, const(double)* y)
>
> I need at least those two implementation for the different cases, a general "default", and for specified types and type qualifications.

'cause your templates are for different types, so they both matches. i wrote only about type deconstruction. the following will work:

 import std.stdio : writeln;
 import std.traits : Unqual;

 auto max(T)(T* x, T* y) if (is(T == Unqual!T)) {
   writeln("General template");
   return *x > *y ? x : y;
 }

 auto max(T)(T* x, T* y) if (is(T == const)) {
   writeln("Const template");
   return *x > *y ? x : y;
 }


 void main () {
   const double p = 2.4, q = 3;
   writeln(max(&p, &q));
 }

note the change in the first template.
March 12, 2017
data pulverizer wrote:

> I need at least those two implementation for the different cases, a general "default", and for specified types and type qualifications.

p.s.: if you want that to work with both pointers and non-pointers, you have to add more constraints, to remove further conflicts.
March 12, 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

You need to make one little change for this to work:

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"
}


The reason this doesn't work is when you use ConstOf!U, it's not looking for a `const U`, it's looking for the type `ConstOf!U`. I'm not sure if this is a bug or not... Anyway, this will also work if we change the following:

void main(){
	ConstOf!double p = 2.4, q = 3; <----- Changed `const double` to `ConstOf!double`
	writeln(max(&p, &q)); //Prints "Const template"
}
March 12, 2017
Meta wrote:

> The reason this doesn't work is when you use ConstOf!U, it's not looking for a `const U`, it's looking for the type `ConstOf!U`. I'm not sure if this is a bug or not...

no, not a bug. this is the way type deconstruction works: it checks if your type was constructed with a given template.
March 12, 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.

March 12, 2017
On Sunday, 12 March 2017 at 20:22:33 UTC, ketmar wrote:
> Meta wrote:
>
>> The reason this doesn't work is when you use ConstOf!U, it's not looking for a `const U`, it's looking for the type `ConstOf!U`. I'm not sure if this is a bug or not...
>
> no, not a bug. this is the way type deconstruction works: it checks if your type was constructed with a given template.

Yeah, it seems to be checking the pattern rather than the type. However, ConstOf!T is just an alias for const(T), but the alias does not seem to be "unwrapped", even though they are supposed to be transparent.
« First   ‹ Prev
1 2