Better handling of noncopyable objects and objects with this(this)

via Digitalmars-d digitalmars-d at puremagic.com
Mon Jun 1 08:37:37 PDT 2015


There is another solution that doesn't require a hidden variable, 
at the cost of duplicating generated code:

     void foo() {
         S s;
         if(cond)
             bar(s);
         some();
         more();
         code();
     }

Can be rewritten as:

     void foo() {
         S s;
         if(cond) {
             bar(s);
             some();
             more();
             code();
             // don't destroy here
         } else {
             some();
             more();
             code();
             s.~this(); // destroy here
         }
     }

But I think this is best left to an optimizer which has more info 
about the costs of the additional code vs the hidden var to 
decide whether its worth it.


More information about the Digitalmars-d mailing list