int foo(int n) { return 1; } int foo(ref int n) { return 2; } int bar()(auto ref int x) { return foo(forward!x); } assert(bar(1) == 1); int i; assert(bar(i) == 2);
void foo(int n, ref string s) { s = null; foreach (i; 0..n) s ~= "Hello"; } // forwards all arguments which are bound to parameter tuple void bar(Args...)(auto ref Args args) { return foo(forward!args); } // forwards all arguments with swapping order void baz(Args...)(auto ref Args args) { return foo(forward!args[$/2..$], forward!args[0..$/2]); } string s; bar(1, s); assert(s == "Hello"); baz(s, 2); assert(s == "HelloHello");
Note: This is just a copy of $(STDREF algorithm, _forward) implementation except it uses fixed move.
Forwards function arguments with saving ref-ness.