[Issue 4668] Troubles with 'auto ref'

via Digitalmars-d-bugs digitalmars-d-bugs at puremagic.com
Wed Dec 9 01:38:49 PST 2015


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

Infiltrator <lt.infiltrator at gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
                 CC|                            |lt.infiltrator at gmail.com
         Resolution|---                         |WORKSFORME

--- Comment #1 from Infiltrator <lt.infiltrator at gmail.com> ---
I'm sure that you probably know this by now, but here goes.


============
int foo(T)(auto ref T[] arr) { // The auto is invalid as we already have ref
T[] to specify the type
    arr.length += 1;
    return 0;
}
void main() {
    foo([1]); // This call will not work because [1] is not an lvalue
    int[] a = [2];
    foo(a);
    auto f = &foo!int;
    int[] b = [3];
    f(b);
    assert(b.length == 1); // This needs to be changed to == 2 to pass on
success
}
============

Modified working version:
============
int foo(T)(ref T[] arr) {
    arr.length += 1;
    return 0;
}
void main() {
    int[] a = [2];
    foo(a);
    auto f = &foo!int;
    int[] b = [3];
    f(b);
    assert(b.length == 2); // Changed to == 2 to pass on success
}
============

--


More information about the Digitalmars-d-bugs mailing list