Destructor order

via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu Oct 23 08:44:26 PDT 2014


On Thursday, 23 October 2014 at 13:03:08 UTC, Marco Leise wrote:
> Am Thu, 23 Oct 2014 12:15:13 +0000
> schrieb "Marc Schütz" <schuetzm at gmx.net>:
>
>> Yet another use case for borrowing.
>
> Cool, how does it keep the original struct alive though, if it
> isn't stored anywhere? Or will it error out when you attempt
> to use the dangling pointer to the object?

It needs to error out. Artificial life support for the original 
object would be too much magic for my taste.

     struct Scoped(T) if(is(T == class)) {
         private T _payload;
         // <insert constructor and destructor here>
         scope!this(T) borrow() { return _payload; }
         alias borrow this;
     }

     void foo() {
         A a = scoped!A();
         // ERROR: cannot assign Scope!A to A
         scope b = scoped!A();
         // ERROR: local `b` outlives temporary
     }


More information about the Digitalmars-d-learn mailing list