digitalmars.D - Ref function pointers?
- Sean Eskapp (2/2) Jan 06 2011 Is there a way to create a function pointer which returns a reference? I...
- Steven Schveighoffer (25/28) Jan 07 2011 int x;
- Simen kjaeraas (7/34) Jan 07 2011 It's a known problem, not sure if it is in Bugzilla though.
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
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
Steven Schveighoffer <schveiguy yahoo.com> wrote:On Thu, 06 Jan 2011 22:37:07 -0500, Sean Eskapp <eatingstaples gmail.com> wrote:It's a known problem, not sure if it is in Bugzilla though. Workaround: alias ref int function() rifn; rifn fn = &foo; -- SimenIs 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.
Jan 07 2011