www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Declaring a pointer to a function returning a ref

reply Jean-Louis Leroy <jl leroy.nyc> writes:
How do I declare a variable that contains a pointer to a function 
returning a reference?

   import std.stdio;

   int foo(return ref int a)
   {
     a = 42;
     return a;
   }

   ref int bar(return ref int a)
   {
     a = 42;
     return a;
   }

   void main()
   {
     int x;
     auto apf = &foo;
     writeln(typeid(apf)); // int function(return ref int)*
     int function(return ref int) xpf = &foo;

     auto apb = &bar;
     writeln(typeid(apb)); // int function(return ref int) ref*

     // int function(return ref int) ref xpb = &bar; // Error: no 
identifier for declarator `int function(return ref int)`
     // ref int function(return ref int) xpb = &bar; // Error: 
variable `castfunc.main.xpb` only parameters or `foreach` 
declarations can be `ref`
   }
Jul 31 2018
next sibling parent Jean-Louis Leroy <jl leroy.nyc> writes:
On Tuesday, 31 July 2018 at 21:29:26 UTC, Jean-Louis Leroy wrote:
 How do I declare a variable that contains a pointer to a 
 function returning a reference?

   import std.stdio;

   int foo(return ref int a)
   {
     a = 42;
     return a;
   }

   ref int bar(return ref int a)
   {
     a = 42;
     return a;
   }

   void main()
   {
     int x;
     auto apf = &foo;
     writeln(typeid(apf)); // int function(return ref int)*
     int function(return ref int) xpf = &foo;

     auto apb = &bar;
     writeln(typeid(apb)); // int function(return ref int) ref*

     // int function(return ref int) ref xpb = &bar; // Error: 
 no identifier for declarator `int function(return ref int)`
     // ref int function(return ref int) xpb = &bar; // Error: 
 variable `castfunc.main.xpb` only parameters or `foreach` 
 declarations can be `ref`
   }
I know I could use `typeof(&bar) xpb = &bar;` but I wonder if there is an explicit syntax.
Jul 31 2018
prev sibling parent John Colvin <john.loughran.colvin gmail.com> writes:
On Tuesday, 31 July 2018 at 21:29:26 UTC, Jean-Louis Leroy wrote:
 How do I declare a variable that contains a pointer to a 
 function returning a reference?

   import std.stdio;

   int foo(return ref int a)
   {
     a = 42;
     return a;
   }

   ref int bar(return ref int a)
   {
     a = 42;
     return a;
   }

   void main()
   {
     int x;
     auto apf = &foo;
     writeln(typeid(apf)); // int function(return ref int)*
     int function(return ref int) xpf = &foo;

     auto apb = &bar;
     writeln(typeid(apb)); // int function(return ref int) ref*

     // int function(return ref int) ref xpb = &bar; // Error: 
 no identifier for declarator `int function(return ref int)`
     // ref int function(return ref int) xpb = &bar; // Error: 
 variable `castfunc.main.xpb` only parameters or `foreach` 
 declarations can be `ref`
   }
alias RefFuncT = ref int function(return ref int a); or alias RefFuncT = typeof(&foo); then RefFuncT xpb = &bar; or you can skip that and do typeof(&bar) xpb = &bar; P.S. next time this sort of question would be more appropriate in the the Learn forum
Jul 31 2018