On Friday, 2 August 2013 at 15:47:49 UTC, monarch_dodra wrote:
On Friday, 2 August 2013 at 15:06:34 UTC, Ivan Kazmenko wrote:
The question is as stated: can a struct be copied by its const
method? I'd expect the answer to be the same for any struct,
but the following two examples say different. I'm using DMD
2.063.2 on Windows.
<...>
To answer real quick, I think the problem is when your type has
aliasing, then dmd has no way of ensuring that after the
postblit, your new struct won't alias any data in the old
struct. Because of this, it is simply not able to generate an
"opAssign(const struct rhs)", and only provides
"opAssign(Struct rhs)".
If you add these implementation yourself:
ref Struct opAssign(const ref Struct rhs)
{
//Pass by ref. make a dup
arr = rhs.arr.dup;
return this;
}
ref Struct opAssign(const Struct rhs)
{
//pass by value
//postblit has already been dup'ed, so we can just force
alias
arr = cast(int[])rhs.arr;
return this;
}
The problem seems to get fixed.
As a side note, implementing these yourself tends to be faster
then relying on a postblit implemented opAssign.
Thank you! After adding the appropriate opAssign-s, everything
seems to work as intended.