digitalmars.D.learn - Function Pointers with Type T
- amparacha (22/22) Sep 15 2014 Can anyone looks at the following code to fix it.I am having
- Adam D. Ruppe (17/17) Sep 15 2014 You can get the code to compile with two changes:
- evilrat (4/9) Sep 16 2014 i wish i knew that back when i needed it, so i could avoid
- amparacha (2/14) Sep 16 2014
- amparacha (4/21) Sep 16 2014 Thanks Adam you saved me from alot.Just one more question how can
- Adam D. Ruppe (13/16) Sep 16 2014 If you want to compare the values, just use them like regular
- ketmar via Digitalmars-d-learn (13/13) Sep 15 2014 On Tue, 16 Sep 2014 01:09:27 +0000
- amparacha (3/17) Sep 16 2014 Thankx
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; }
Sep 15 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.
Sep 15 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 :(
Sep 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 :(
Sep 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=∁ 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.
Sep 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
Sep 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=3D∁ to auto ptr =3D &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 rig= ht){
Sep 15 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=∁ 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){
Sep 16 2014