Why can't we make reference variables?

Timon Gehr timon.gehr at gmx.ch
Tue Aug 28 18:42:35 PDT 2012


Not exactly the same thing (what you propose would have different
IFTI behaviour), but works quite well:

import std.stdio;

struct Ref(T){
     private T* _payload;
     this(ref T i){_payload = &i; }
     @property ref T deref(){ return *_payload; }
     alias deref this;
}
auto ref_(T)(ref T arg){return Ref!T(arg);}

void main(){
     int i,j;
     auto r = i.ref_;
     r++;
     auto q = r;
     writeln(r," ",q);
     q++;
     writeln(r," ",q);
     q = j.ref_;
     q++;
     writeln(r," ",q.deref);
}


More information about the Digitalmars-d mailing list