Consider the following struct:
struct S
{
int *val;
ref int get() { return *val; }
}
void foo(ref int x) {}
void main()
{
int x;
foo(S(&x).get);
}
This builds fine. However, using alias this to forward to the get function:
struct S
{
int *val;
ref int get() { return *val; }
alias get this;
}
void foo(ref int x) {}
void main()
{
int x;
foo(S(&x));
}
I get:
onlineapp.d(15): Error: function onlineapp.foo(ref int x) is not callable using argument types (S)
onlineapp.d(15): cannot pass rvalue argument S(& x) of type S to parameter ref int x
Clearly, the alias this should work, especially since if I call the aliased member directly it does work.
This is a block for creating correct smart reference types.
Comment #1 by b2.temp — 2021-05-08T15:46:01Z
The problem is likely the use of `Type.aliasthisOf()` which returns a `Type`, so for a function this is TypeFunction.nextOf() and as `ref` is not a type qual, the information is lost and DMD thinks it's a rvalue.
Comment #2 by apz28 — 2021-05-08T19:41:27Z
Create temp var is OK
struct S
{
int *val;
ref int get() { return *val; }
alias get this;
}
void foo(ref int x) {}
void main()
{
int x;
auto s = S(&x);
foo(s);
}
Comment #3 by schveiguy — 2021-05-11T13:53:26Z
(In reply to apham from comment #2)
> Create temp var is OK
That doesn't trigger the bug because then s is an lvalue.
Comment #4 by robert.schadek — 2024-12-13T19:12:31Z