[Issue 4668] New: Troubles with 'auto ref'

d-bugmail at puremagic.com d-bugmail at puremagic.com
Tue Aug 17 08:11:20 PDT 2010


http://d.puremagic.com/issues/show_bug.cgi?id=4668

           Summary: Troubles with 'auto ref'
           Product: D
           Version: D2
          Platform: x86
        OS/Version: Windows
            Status: NEW
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: nobody at puremagic.com
        ReportedBy: bearophile_hugs at eml.cc


--- Comment #0 from bearophile_hugs at eml.cc 2010-08-17 08:11:13 PDT ---
Few examples of 'auto ref' usage that I don't understand, so I mix them here.

This D2 code compiles and runs with no errors (DMD 2.048), showing that that
'f' is the version without 'ref':


int foo(T)(auto ref T[] arr) {
    arr.length += 1;
    return 0;
}
void main() {
    foo([1]);
    int[] a = [2];
    foo(a);
    auto f = &foo!int;
    int[] b = [3];
    f(b);
    assert(b.length == 1);
}


How can I associate to 'f' the version with 'ref'?

----------------

If in that program I comment out the first line of the main:


int foo(T)(auto ref T[] arr) {
    arr.length += 1;
    return 0;
}
void main() {
    //foo([1]);
    int[] a = [2];
    foo(a);
    auto f = &foo!int;
    int[] b = [3];
    f(b);
    assert(b.length == 1);
}


Then it asserts, b.length is now 2. So 'f' is the version with 'ref'.

----------------

If in the original program I comment out the whole first part of the main():


int foo(T)(auto ref T[] arr) {
    arr.length += 1;
    return 0;
}
void main() {
    /*
    foo([1]);
    int[] a = [2];
    foo(a);
    */
    auto f = &foo!int;
    int[] b = [3];
    f(b);
    assert(b.length == 1);
}


I receive error messages at compile-time (but I don't understand the first
error message):

test.d(1): Error: auto can only be used for template function parameters
test.d(11): Error: template instance test9.foo!(int) error instantiating

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------


More information about the Digitalmars-d-bugs mailing list