rvalues -> ref (yup... again!)

Dgame r.schuett.1987 at gmail.com
Thu Mar 29 19:11:30 UTC 2018


Just to be sure it does not got los: You know that you can avoid 
the temp/copy if you add one method to your struct, yes?

----
import std.stdio;

struct Big {
     string name;
     float[1000] values;

     this(string name) {
         this.name = name;
     }

     @disable
     this(this);

     ref auto byRef() inout {
         return this;
     }
}

void foo(ref const Big b) {
     writeln(b.name);
}

void main() {
     Big b = Big("#1");
     foo(b);
     foo(Big("#2").byRef);
}
----

That works like a charm and avoids any need for rvalue 
references. Just add it as mixin template in Phobos or something 
like that.


More information about the Digitalmars-d mailing list