www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Ref function pointers?

reply Sean Eskapp <eatingstaples gmail.com> writes:
Is there a way to create a function pointer which returns a reference? In all
the ways I've tried it, my return value becomes "not an lvalue".
Jan 06 2011
parent reply "Steven Schveighoffer" <schveiguy yahoo.com> writes:
On Thu, 06 Jan 2011 22:37:07 -0500, Sean Eskapp <eatingstaples gmail.com>  
wrote:

 Is there a way to create a function pointer which returns a reference?  
 In all
 the ways I've tried it, my return value becomes "not an lvalue".
int x; ref int foo() { return x; } void main() { auto func = &foo; pragma(msg, typeof(func).stringof); func() = 5; } compiles and outputs during compilation: int function() ref However, doing this: int function() ref func = &foo; results in an error: testreffuncptr.d(9): no identifier for declarator int function() testreffuncptr.d(9): semicolon expected, not 'ref' testreffuncptr.d(9): found 'ref' instead of statement So, yes, you can make one. No you can't specifically type it ;) You must use auto. I find the type string extremely strange too. I think this is bugzilla-worthy. -Steve
Jan 07 2011
parent "Simen kjaeraas" <simen.kjaras gmail.com> writes:
Steven Schveighoffer <schveiguy yahoo.com> wrote:

 On Thu, 06 Jan 2011 22:37:07 -0500, Sean Eskapp  
 <eatingstaples gmail.com> wrote:

 Is there a way to create a function pointer which returns a reference?  
 In all
 the ways I've tried it, my return value becomes "not an lvalue".
int x; ref int foo() { return x; } void main() { auto func = &foo; pragma(msg, typeof(func).stringof); func() = 5; } compiles and outputs during compilation: int function() ref However, doing this: int function() ref func = &foo; results in an error: testreffuncptr.d(9): no identifier for declarator int function() testreffuncptr.d(9): semicolon expected, not 'ref' testreffuncptr.d(9): found 'ref' instead of statement So, yes, you can make one. No you can't specifically type it ;) You must use auto. I find the type string extremely strange too. I think this is bugzilla-worthy.
It's a known problem, not sure if it is in Bugzilla though. Workaround: alias ref int function() rifn; rifn fn = &foo; -- Simen
Jan 07 2011