Passing Templated Function Arguments Solely by Reference

"Nordlöw" via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue Jul 8 17:13:39 PDT 2014


If I want randInPlace to take value arguments (such as structs) 
by reference and reference types (classes) as normal is this

/** Generate Random Contents in $(D x).
  */
auto ref randInPlace(T)(auto ref T x) @safe /* nothrow */ if 
(isIterable!T)
{
     foreach (ref elt; x)
     {
         import std.range: ElementType;
         static if (isInputRange!(ElementType!T))
             elt[].randInPlace;
         else
             elt.randInPlace;
     }
     return x;
}

the way to do it or does

     auto ref T x

evaluate to unnecessary

     ref Class x

?

And isIterable the recommended way to do it?

And how does this compare to using x[].randInPlace() when x is a 
static array? Does x[] create unnecessary GC-heap activity in 
this case?

I'm wondering because (auto ref T x) is just used in two places 
in std.algorithm and std.range in Phobos. Is this a relatively 
new enhancement?


More information about the Digitalmars-d-learn mailing list