Manually allocate delegate?

Baz via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sun Jul 12 04:22:39 PDT 2015


On Sunday, 12 July 2015 at 10:39:44 UTC, Tofu Ninja wrote:
> On Sunday, 12 July 2015 at 10:19:02 UTC, Baz wrote:
>> [...]
>
> That is not manually allocating a delegate context, and & in 
> that instance does not even allocate. For delegates to class 
> methods, the context is just the "this" pointer of the object, 
> so the context in that code is just foo, which you still 
> allocated on the gc. The delegate itself (the function pointer 
> and the context pointer) is just allocated on the stack.
>
> What I am talking about is the context that implicitly gets 
> allocated when you make a delegate to a nested function.
>
> void main(string[] args)
> {
> 	auto d = bar();
> 	d();
> }
>
>
> auto bar()
> {
> 	int x = 5;
> 	
> 	void foo()
> 	{
> 		writeln(x);
> 	}
> 	
> 	auto d = &foo; // <-- allocates
> 	return d;
> }

At least now your Question is clearer and understandable...but 
sorry goodbye. I don't feel good vibes here. See ya ^^.



More information about the Digitalmars-d-learn mailing list