scope guard question

Steven Schveighoffer schveiguy at gmail.com
Mon Jun 29 22:47:16 UTC 2020


On 6/29/20 6:31 PM, Arjan wrote:
> ```
> void main()
> {
>    import std.stdio;
>    auto f = (){
>      string[] t;
>      { // inner scope
>        t ~= "hello";
>        scope( exit ) t ~= "world";
>      } // inner scope exit
>      return t;
>    };
> 
>    f().writeln; // ["hello", "world"]
> }
> ```
> removing the inner scope in f() gives ["hello"]
> 
> So when no inner scope is present, the scope exit 'runs' after the 
> return? Is that indeed expected behavior according to the specification?

Yes. The return statement is inside the scope of the function, so it 
runs before the scope is exited. Are you saying the spec doesn't say that?

-Steve


More information about the Digitalmars-d-learn mailing list