[Issue 24612] New: Explicitly given `auto ref` parameter can’t bind by lvalue by value

d-bugmail at puremagic.com d-bugmail at puremagic.com
Sun Jun 16 11:37:50 UTC 2024


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

          Issue ID: 24612
           Summary: Explicitly given `auto ref` parameter can’t bind by
                    lvalue by value
           Product: D
           Version: D2
          Hardware: All
                OS: All
            Status: NEW
          Severity: enhancement
          Priority: P1
         Component: dmd
          Assignee: nobody at puremagic.com
          Reporter: qs.il.paperinik at gmail.com

```d
void f(T)(auto ref T x) {}

void main()
{
    int x;
    f!long(x); // Error: template `f` is not callable using argument types
`!(long)(int)`
}
```

This should instantiate `f` as `f!long(long x)`, as `int` converts to `long`
and `cast(long)x` is an rvalue.

I don’t know how far with implicit conversions this should go, but it should
work for built-in integer and floating-point types. It should also work for
class/interface types with inheritance relationship, and while binding those by
`auto ref` is weird to do explicitly, such a binding might end up from
templates that bind arbitrary type objects that happen to be class handles in
the user’s code. Then:
```d
Exception e = new Exception("");
f!Object(e); // Currently error, should be pass-by-value
```

With my proposed fix for Issue 24611, users could opt-into `ref` explicitly if
they need to.

--


More information about the Digitalmars-d-bugs mailing list