ref is unsafe

Tommi tommitissari at hotmail.com
Thu Jan 10 08:42:08 PST 2013


...Although, I should add that my analogy between methods and 
free functions seems to break when the object is an rvalue. Like 
in:

struct T
{
     int v;

     this(int a)
     {
         v = a;
     }

     int get()
     {
         return v;
     }
}

int v = T(4).get();

Given my analogy, the method get() should be able to be thought 
of as a free function:

int gget(ref T obj)
{
     return obj.v;
}

But then the above method call should be able to thought of as:

int v = gget(T(4));

...which won't compile because T(4) is an rvalue, and according 
to D, rvalues can't be passed as ref (nor const ref). I don't 
know which one is flawed, my analogy, or the logic of how D is 
designed.



More information about the Digitalmars-d mailing list