Thread overview
Function Pointers with Type T
Sep 16, 2014
amparacha
Sep 16, 2014
Adam D. Ruppe
Sep 16, 2014
evilrat
Sep 16, 2014
amparacha
Sep 16, 2014
amparacha
Sep 16, 2014
Adam D. Ruppe
Sep 16, 2014
ketmar
Sep 16, 2014
amparacha
September 16, 2014
Can anyone looks at the following code to fix it.I am having
error when using pointers to functions with argument of type
T.This is a small portion of a program doing a generic quick sort
by passing a comparison function as an argument.

import std.stdio;


int main(){

return 0;
}

bool comp(T)(T left,T right){
//some comparison criteria
return false;
}


void sort(T)(T[]list,int left,int right)
{
int spiltPoint;
bool function(T)(T val1,T val2) ptr=∁
spiltPoint=partition(list,ptr,left,right);	
}

int partition(T)(T[]list,bool function(T)(T val1,T val2)ptr,int
left,int right){

return 2;

}
September 16, 2014
You can get the code to compile with two changes:

bool function(T)(T val1,T val2) ptr=∁

should be:

bool function(T val1,T val2) ptr=&comp!T;



The function pointer itself isn't a template, so it doesn't need the (T) parameter. Instead, since it is inside a template, you can just use the T from the outside directly.

Moreover, comp has compile time arguments, so you can't take the address of it without forwarding the arguments. So instead of &comp, you use &comp!T - passing the T from the outside to the comparison function too.

and also

int partition(T)(T[]list,bool function(T)(T val1,T val2)ptr,int

should be:

int partition(T)(T[]list,bool function(T val1,T val2)ptr,int


Again because the pointer isn't a new template, it should just use the type T from the outer argument list.
September 16, 2014
On Tue, 16 Sep 2014 01:09:27 +0000
amparacha via Digitalmars-d-learn <digitalmars-d-learn@puremagic.com>
wrote:

first change:
  bool function(T)(T val1,T val2) ptr=&comp;
to
  auto ptr = &comp!T;

second change:
  int partition(T)(T[]list,bool function(T)(T val1,T val2)ptr,int left,int right)}
to
  int partition(T)(T[]list,bool function(T val1,T val2)ptr,int left,int right){



September 16, 2014
On Tuesday, 16 September 2014 at 01:16:14 UTC, Adam D. Ruppe wrote:
>
> bool function(T val1,T val2) ptr=&comp!T;
>
>
> Moreover, comp has compile time arguments, so you can't take the address of it without forwarding the arguments. So instead of &comp, you use &comp!T - passing the T from the outside to the comparison function too.

i wish i knew that back when i needed it, so i could avoid spaghetti mess and not abandon my code :(
September 16, 2014
Thanks Adam you saved me from alot.Just one more question how can
I compare two arguments of type T.
On Tuesday, 16 September 2014 at 01:16:14 UTC, Adam D. Ruppe
wrote:
> You can get the code to compile with two changes:
>
> bool function(T)(T val1,T val2) ptr=&comp;
>
> should be:
>
> bool function(T val1,T val2) ptr=&comp!T;
>
>
>
> The function pointer itself isn't a template, so it doesn't need the (T) parameter. Instead, since it is inside a template, you can just use the T from the outside directly.
>
> Moreover, comp has compile time arguments, so you can't take the address of it without forwarding the arguments. So instead of &comp, you use &comp!T - passing the T from the outside to the comparison function too.
>
> and also
>
> int partition(T)(T[]list,bool function(T)(T val1,T val2)ptr,int
>
> should be:
>
> int partition(T)(T[]list,bool function(T val1,T val2)ptr,int
>
>
> Again because the pointer isn't a new template, it should just use the type T from the outer argument list.
September 16, 2014
Thanks its the right help at right time.
On Tuesday, 16 September 2014 at 09:13:38 UTC, evilrat wrote:
> On Tuesday, 16 September 2014 at 01:16:14 UTC, Adam D. Ruppe wrote:
>>
>> bool function(T val1,T val2) ptr=&comp!T;
>>
>>
>> Moreover, comp has compile time arguments, so you can't take the address of it without forwarding the arguments. So instead of &comp, you use &comp!T - passing the T from the outside to the comparison function too.
>
> i wish i knew that back when i needed it, so i could avoid spaghetti mess and not abandon my code :(
September 16, 2014
Thankx
On Tuesday, 16 September 2014 at 01:21:57 UTC, ketmar via Digitalmars-d-learn wrote:
> On Tue, 16 Sep 2014 01:09:27 +0000
> amparacha via Digitalmars-d-learn <digitalmars-d-learn@puremagic.com>
> wrote:
>
> first change:
>   bool function(T)(T val1,T val2) ptr=&comp;
> to
>   auto ptr = &comp!T;
>
> second change:
>   int partition(T)(T[]list,bool function(T)(T val1,T val2)ptr,int left,int right)}
> to
>   int partition(T)(T[]list,bool function(T val1,T val2)ptr,int left,int right){

September 16, 2014
On Tuesday, 16 September 2014 at 17:32:02 UTC, amparacha wrote:
> Thanks Adam you saved me from alot.Just one more question how can
> I compare two arguments of type T.

If you want to compare the values, just use them like regular variables. If you want to compare the types, use:

static if(is(T == R)) { }

or one of the other forms here http://dlang.org/expression.html#IsExpression

For example:

bool typesMatch(T, R)() {
   static if(is(T == R)) return true;
   return false;
}

writeln(typesMatch!(int, float)); // false
writeln(typesMatch!(int, int)); // true