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

Rubn where at is.this
Thu Mar 29 20:05:48 UTC 2018


On Thursday, 29 March 2018 at 19:11:30 UTC, Dgame wrote:
> 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.

Doesn't work with built-in types like float.

Just adds bloat for operators like opBinary if you want that to 
be ref.

foo((a.byRef + b.byRef * c.byRef).byRef)

// vs

foo(a + b * c);

It's kind of funny all this talk about allowing temporaries to 
bind to refs being messy, yet you can already bind a temporary to 
a ref in a messy way using that.




More information about the Digitalmars-d mailing list