What's this? (Was: DMD 1.028 and 2.012 releases)
Steven Schveighoffer
schveiguy at yahoo.com
Fri Mar 7 08:13:16 PST 2008
"Aarti_pl" wrote in message
> Did you notice this example in docs?
> (http://www.digitalmars.com/d/2.0/struct.html#AssignOverload)
>
>
> S* opAssign(S s)
> { S tmp <== *this; // bitcopy *this into tmp
> *this <== s; // bitcopy s into *this
> tmp.~this(); // call destructor on tmp
> return this;
> }
>
> It seems to be something new in D 2.012. Does it work? How?
>
> BR
> Marcin Kuszczak
> (aarti_pl)
After reading this new feature, I'm really confused. I like the feature,
but I'm not sure how it works, or why there are two functions for the same
thing.
How does one use the postblit, and how does one use the opAssign? It seems
they both serve the same function, but opAssign allows more control
possibly? And does opAssign get called if the postblit doesn't exist or
vice versa?
Not to knock what you have done, but it would seem to me more reasonable to
have a single method, and if you wanted to blit the values before your
custom code, just do:
S* opAssign(ref const S s)
{
*this = s;
...
}
And this would get called on both copy construction and assignment. The way
I see it now (if each are individual operations), blitting won't work if the
source is const and contains a pointer, so that the following confusing
situation happens:
struct S
{
int[] buf
this(this)
{
buf = buf.dup;
}
S* opAssign(ref const S s)
{
buf = s.buf.dup;
return this;
}
}
const S s;
S t = s; // fails because the blit fails
S u;
u = s; // works because no blit occurred
Also it appears that your "equivalent" statement is wrong, or else the
opAssign signature is wrong:
t = S.opAssign(s);
Doesn't t need to be a struct pointer? Or else is opAssign not really
returning a struct pointer? Does this work?
S u;
u = t = s;
Finally, I don't understand the reasoning for tmp. It appears to be an
unused part of the default opAssign...
-Steve
More information about the Digitalmars-d-announce
mailing list