[Issue 8845] Can't pass immediate or rvalue args to ref function parameters

via Digitalmars-d-bugs digitalmars-d-bugs at puremagic.com
Sat Dec 3 09:58:57 PST 2016


https://issues.dlang.org/show_bug.cgi?id=8845

--- Comment #21 from Nick Treleaven <nick at geany.org> ---
I came up with a lvalueOf!rvalue template which hopefully is memory-safe. Not
tested with Walter's DIP1000 scope branch (yet).

enum typeof(v[0])[v.length] staticArray(v...) = [v];

ref const(T) lvalueOf(alias rvalue, T=typeof(rvalue))
    (return ref T rv = *staticArray!rvalue.ptr) @trusted
{
    return rv;
}

auto foo(ref const int i) {assert(i == -1);}

@safe @nogc unittest
{
    static assert(!__traits(compiles, foo(0-1)));
    foo(lvalueOf!(0-1));
}

--


More information about the Digitalmars-d-bugs mailing list