does 'auto ref T' copy or not if passed rvalue

Dan dbdavidson at yahoo.com
Mon Dec 17 09:21:38 PST 2012


For the code below, S is never copied even though a version of 
the function resolving to void x.foo!(x.S).foo(x.S) is called. 
How is this possible?
Is it an optimization? I figure if I call a function that takes 
its parameter by value, there should be a copy.

Thanks
Dan

--------------- Output -----------------
T: abc is ref
T: xyz is not ref
--------------- Code -----------------
import std.stdio;

struct S {
   char c[];
   this(this) {writeln("Copying S\n");}
}

void foo(T)(auto ref T t) {
   writeln("T: ", t.c, (__traits(isRef, t) ? " is ref " : " is not 
ref "));
}

void main() {
   S s = { ['a','b','c'] };
   foo(s);
   foo(S(['x', 'y', 'z']));
}
--------------

nm -C x | ddmangle | grep foo
0000000000477ab4 W void x.foo!(x.S).foo(ref x.S)
00000000004783fc W void x.foo!(x.S).foo(x.S)


More information about the Digitalmars-d-learn mailing list