opAssign and const?

Era Scarecrow rtcvb32 at yahoo.com
Fri May 4 16:18:13 PDT 2012


On Friday, 4 May 2012 at 21:29:14 UTC, Steven Schveighoffer wrote:
> On Fri, 04 May 2012 17:26:02 -0400, Era Scarecrow
>> On Friday, 4 May 2012 at 21:12:55 UTC, Steven Schveighoffer 
>> wrote:
>>> y[] = x2.y[];
>>> // as you did below
>>
>> That may deal with the language requirements, but the ideal  
>> was to take over the temporary; If not the ref would copy the  
>> contents. I wonder if I will need to have 2 copies of each  
>> opAssign to satisfy everything.

> OK, I see what you are trying to do.  I'll have to think about  
> it some more.  I'm very concerned that you could potentially  
> end up calling this function with an lvalue, which would make  
> this a disastrous strategy.

  Well let's see if we can recap it into perspective so we're on 
the same Page. I'm trying to make a struct that on assignment 
will copy it's contents.

  On opAssign In both cases the mutable and const/immutable rvalue 
will copy it's contents. If it's a temporary (which is always 
returned as mutable) I want to take it over rather than copy the 
contents. The contents are likely referenced elsewhere already.

  Here's a stripped version of my struct, obviously with lots of 
missing unneeded stuff.

struct X {
   ubyte[] buffer;

   //from temporary being returned.
   ref X opAssign(X rhs) {
     this.buffer = rhs.buffer;
     return this;
   }

   //copying from right side to left.
   ref X opAssign(ref X rhs) {
     if (buffer.length != rhs.length) {
       buffer.length = rhs.buffer.length;
       ptr = buffer.ptr;
     }

     buffer[] = rhs.buffer[];
     return this;
   }
}


More information about the Digitalmars-d-learn mailing list