Copy elision by spec

Lars T. Kyllingstad public at kyllingen.net
Sun Nov 3 23:02:20 PST 2013


I was quite surprised to see that the following program compiles 
just fine with DMD:

     struct S
     {
         @disable this(this);
         int n;
     }

     S createS(int i)
     {
         S s;
         s.n = i;
         return s;
     }

     void main(string[] args)
     {
         auto foo = createS(1);
         foo = createS(2);
     }

I already knew that the compiler was allowed to elide copies on 
return from functions, but I thought this was an optimisation, 
and not part of the language proper.  I would have expected the 
compiler to complain that createS() can't return an S since S's 
postblit constructor is disabled.

My question is therefore, is this by design?  Can I rely on this 
to work in the future, and on all compilers?  If this is the 
case, it really should be added to the spec.  (Or maybe it's 
there already, but I couldn't find it.)

Lars


More information about the Digitalmars-d mailing list