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

Nick Treleaven nick at geany.org
Thu Mar 29 11:43:36 UTC 2018


On Saturday, 24 March 2018 at 17:34:09 UTC, Manu wrote:
> You mutate a temporary that times out at the end of the 
> statement...
> your statement is never assigned to anything, and has no effect.

That is solved by having the ref function return its argument (so 
it can be chained):

struct S;
ref S modify(return ref S s);

S(data).modify.writeln;

This rvalue pattern would be disallowed if argument `s` was 
const. Why not make `modify` just return a copy then, maybe the 
optimizer could remove the copy? So that you can also use it with 
lvalues:

S s;
modify(s);


More information about the Digitalmars-d mailing list