why @property cannot be pass as ref ?
Ali Çehreli
acehreli at yahoo.com
Wed Dec 20 18:04:57 UTC 2017
On 12/20/2017 07:02 AM, ChangLong wrote:
> =======
> struct A {
> alias This = typeof(this) ;
>
> void opAssign(ref This ){
> }
>
> ref auto left(){
> return This() ;
> }
> }
>
> void main(){
> A root ;
> ref find() {
> auto p = root ;
> p = p.left;
> }
> }
> ===========
>
> test.d(16): Error: function tree.A.opAssign (ref A _param_0) is not
> callable using argument types (A)
>
>
> Is this by design ?
The problem is not with opAssign but with left(), which returns an
rvalue. It's by design that rvalues cannot be bound to references in D.
The code compiles if left() returns an lvalue:
ref auto left(){
// return This() ;
return *l;
}
This *l;
What you can do is to convert opAssign to a template with an additional
set of parameters and change its parameter to 'auto ref':
void opAssign()(auto ref This ){
}
'auto ref' essentially generates two copies of the function: one taking
by-value for rvalues and one taking by-ref for lvalues. So, be aware
that you cannot use the address of the argument because in the case of
rvalues its a local variable (copy of the actual argument).
Ali
More information about the Digitalmars-d-learn
mailing list