Function Pointers with Type T

evilrat via Digitalmars-d digitalmars-d at puremagic.com
Mon Sep 15 09:51:23 PDT 2014


On Monday, 15 September 2014 at 16:28:19 UTC, amparacha wrote:
> 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;
>
> }

this T function is just short form of template which expands to
----
template partition(T)
{
  int partition(T[] list, bool function(T val1, T val2) ptr, int 
left, int right)
  {
   return 2;
  }
}
----
cannot alias template function, so in fact the closest thing is 
use alias template parameter. try this(not tested):
---------------
import std.stdio;


void main(){
cast(void)0;
}

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

void sort(T)(T[]list,int left,int right)
{
int spiltPoint;
spiltPoint=partition!(T, comp)(list,left,right);	
}

int partition(T, alias compFunc)(T[] list, int left, int right)
{
return 2;
}

alias compFunc(T) = bool function (T val1,T val2) ;
-----
the only problem(?) involving template alias makes this 
impossible(?) to call at run time(CTFE only)


More information about the Digitalmars-d mailing list