auto used as scope

Lionello Lunesu lio at lunesu.remove.com
Wed Apr 18 06:53:18 PDT 2007


Martin Persenius wrote:
> Hi!
> 
> There is inconsistency in the usage of 'auto', where it actually behaves as 'scope'. According to users of #d this might be because auto comes from scope. I would like to understand why the dtor is and is not called in the following cases, and the reasoning for having it that way.
> 
> import std.stdio;
> 
> class Foo {
>     this() {
>         writef("0");
>     }
> 
>     ~this() {
>         writef("1");
>     }
> }
> 
> int main() {
>     try {
>         scope(failure) writef("4");
>         scope(exit) writef("2");
>         auto Foo f = new Foo();
>         throw new Exception("msg");

Perhaps the writef does some allocation that will detect the 
disappearance of any pointer to your Foo, thus collecting the object and 
running its dtor? It sounds probable, the first time writef is run it 
could allocate some buffer.

L.



More information about the Digitalmars-d mailing list