Thread overview | ||||||||
---|---|---|---|---|---|---|---|---|
|
September 15, 2014 Function Pointers with Type T | ||||
---|---|---|---|---|
| ||||
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 15, 2014 Re: Function Pointers with Type T | ||||
---|---|---|---|---|
| ||||
Posted in reply to amparacha | 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;
>
> }
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.
|
September 15, 2014 Re: Function Pointers with Type T | ||||
---|---|---|---|---|
| ||||
Posted in reply to amparacha | 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 = ∁
spiltPoint = partition(list, ptr, left, right);
}
void main() {}
Bye,
bearophile
|
September 15, 2014 Re: Function Pointers with Type T | ||||
---|---|---|---|---|
| ||||
Posted in reply to amparacha | 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)
|
September 16, 2014 Re: Function Pointers with Type T | ||||
---|---|---|---|---|
| ||||
Posted in reply to evilrat |
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=∁
>> 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)
|
September 16, 2014 Re: Function Pointers with Type T | ||||
---|---|---|---|---|
| ||||
Posted in reply to amparacha | 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
}
|
Copyright © 1999-2021 by the D Language Foundation