How to fix opAssign signature
Dan
dbdavidson at yahoo.com
Wed Nov 7 09:52:43 PST 2012
On Friday, 2 November 2012 at 15:56:47 UTC, Ali Çehreli wrote:
>
> ref D opAssign(D other) {
> swap(c, other.c);
> return this;
> }
>
> There may be corner cases where this is not efficient, but
> considering that assignment involves two sub-operations (make a
> copy of the new state and destroy the old state), the above is
> doing exactly that. (It is the same idiom for strongly
> exception-safe operator= in C++.)
>
> That has been the observation that led me to understand that
> by-value is the way to go with struct opAssign. Please let us
> know whether it has weaknesses. :)
>
[snip]
> Ali
Neat trick. But how do you deal with this?
D d1;
const(D) d2;
d1 = d2
As soon as I add an assoc array to A I need to implement an
opAssign if I want to be able to assign a const(A) to an A. And I
would want to be able to do that for this composition. Note the
cast - is that safe?
import std.stdio;
struct B {
private A _a;
@property auto a(const ref A other) {
_a = other;
}
}
struct A {
int[1024] i; /// Very big making pass by value bad choice
string[string] h;
auto opAssign(const ref A other) {
h = cast(typeof(h))other.h.dup;
}
}
void main() {
B b;
A a = { [0], [ "foo" : "bar" ] };
b.a = a;
}
Thanks
Dan
More information about the Digitalmars-d-learn
mailing list