std.algorithm.move
Andrej Mitrovic
andrej.mitrovich at gmail.com
Tue Aug 17 10:53:23 PDT 2010
In TDPL page 251 there's some piece of code and an explanation where we can force the compiler to do a move on a struct instead of a copy. If I understood right, then I think there might be a bug here:
import std.algorithm, std.stdio;
struct Widget
{
private int[] array;
this(uint length)
{
array = new int[length];
}
// postblit constructor
this(this)
{
array = array.dup;
}
int get(size_t offset)
{
return array[offset];
}
void set(size_t offset, int value)
{
array[offset] = value;
}
}
void kun(Widget w)
{
}
unittest
{
auto w = Widget(10);
// Call to move inserted
kun(move(w)); // w will be moved,
// an empty default-constructed Widget
// replaces w's contents
assert(w == Widget.init); // Passes
}
void main()
{
}
The function kun() does get the moved struct, and not a copy, like intended. But the assert does not pass. If I change kun to this:
void kun(Widget w)
{
w.set(2, 40);
}
then in the unittest I will see the changes if I print out the contents of w.
>From what I understand, in this code we want to move a struct and not copy it, but we don't want "w" in the unittest to refer to the same memory anymore, so w should get initialized with it's init value.
But I'm not seeing this behavior in the code. Is this a bug?
More information about the Digitalmars-d
mailing list