digitalmars.D.bugs - [Issue 4668] New: Troubles with 'auto ref'
- d-bugmail puremagic.com (70/70) Aug 17 2010 http://d.puremagic.com/issues/show_bug.cgi?id=4668
 
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 puremagic.com
        ReportedBy: bearophile_hugs eml.cc
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: -------
 Aug 17 2010








 
 
 
 d-bugmail puremagic.com