Const ref and rvalues again...

Dmitry Olshansky dmitry.olsh at gmail.com
Sun Nov 11 01:36:05 PST 2012


11/10/2012 11:07 PM, Jonathan M Davis пишет:
> And actually, to make matters worse, I'm not sure that scope on delegates is
> working correctly. I thought that it was, but this code compiles:
>
> import std.stdio;
>
> void delegate() global;
>
> void foo(scope void delegate() del)
> {
>      global = del;
> }
>
> void main()
> {
>      {
>          char[5] bar = "hello";
>          foo((){writeln(bar);});
>      }
>      char[7] baz = "goodbye";
>
>      global();
> }
>
> It also prints out "hello", and if a closure had not been allocated,
I would
> have at least half-expected it to print out "goodb", because I'd have thought
> that baz would have been taking up the same memory that bar had been.

Nope. It's just that the stack is intact and contains: hello and goodbye 
one after another. Without optimizations { } scope doesn't mean reuse 
stack space.

Now if play with stack a bit, for me the next one prints:
­-²↑

import std.stdio;

void delegate() global;

void foo(scope void delegate() del)
{
     global = del;
}


void f()
{
     {
         char[5] bar = "hello";
         foo((){writeln(bar);});
     }
}

void main()
{
     char[7] baz = "goodbye";
     f();

     global();
}

-- 
Dmitry Olshansky


More information about the Digitalmars-d mailing list