## digitalmars.D - Function Pointers with Type T

• amparacha (22/22) Sep 15 2014 Can anyone looks at the following code to fix it.I am having
• Phil Lavoie (6/28) Sep 15 2014 At first glance it looks like you have a syntax error:
• bearophile (21/25) Sep 15 2014 import std.stdio;
• evilrat (36/58) Sep 15 2014 this T function is just short form of template which expands to
• amparacha (2/70) Sep 15 2014
• evilrat (10/11) Sep 15 2014 what do you mean? notice this is CTFE
"amparacha" <paracham mcmaster.ca> writes:
```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=&comp;
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;

}
```
Sep 15 2014
"Phil Lavoie" <maidenphil hotmail.com> writes:
```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=&comp;
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;

}

At first glance it looks like you have a syntax error:
bool function(T)(T val1, T val2) ptr ...
should be:
auto ptr = &(comp!(T)); //Instantiate the template function
first. Auto deduces the type.
```
Sep 15 2014
"bearophile" <bearophileHUGS lycos.com> writes:
```amparacha:

Such questions are better asked in the D.learn newsgroup.

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;

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

int partition(T)(T[] list,
bool function(T val1, T val2) f,
int left,
int right) {
return 2;
}

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

void main() {}

Bye,
bearophile
```
Sep 15 2014
"evilrat" <evilrat666 gmail.com> writes:
```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=&comp;
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)
```
Sep 15 2014
"amparacha" <paracham mcmaster.ca> writes:
```thankx but all of these are not working
On Monday, 15 September 2014 at 16:51:24 UTC, evilrat wrote:
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=&comp;
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)

```
Sep 15 2014
"evilrat" <evilrat666 gmail.com> writes:
```On Tuesday, 16 September 2014 at 01:08:14 UTC, amparacha wrote:
thankx but all of these are not working

what do you mean? notice this is CTFE
it should pick compare function at compile time, but arguments
may be passed at run time. so call it using enum
void sort...
{
enum result = splitPoint!(T, cmpfunc)(list, left, right); // ctfe
part
... use result ... // run time
}
```
Sep 15 2014