digitalmars.D - What does the following program do?
- Shachar Shemesh (41/41) Dec 05 2017 import std.algorithm: move;
- Nemanja Boric (11/55) Dec 05 2017 ```
- =?UTF-8?Q?Ali_=c3=87ehreli?= (8/10) Dec 05 2017 The same issue came up recently in the learn group regarding
import std.algorithm: move; import std.stdio; import std.string; class A { int val; override string toString() const { return "A(%s)".format(val); } } struct B { int val; } void main() { B b = B(12); B* bp = &b; B* bp2; writefln("bp=%s bp2=%s", bp, bp2); move( bp, bp2 ); writefln("bp=%s bp2=%s", bp, bp2); A a1 = new A(); a1.val = 42; A a2; writefln("a1=%s (%s) a2=%s (%s)", a1, cast(void*)a1, a2, cast(void*)a2); move( a1, a2 ); writefln("a1=%s (%s) a2=%s (%s)", a1, cast(void*)a1, a2, cast(void*)a2); B b2; writefln("b=%s b2=%s", b, b2); move( b, b2 ); writefln("b=%s b2=%s", b, b2); } Answer: bp=7FFFAB559970 bp2=null bp=7FFFAB559970 bp2=7FFFAB559970 a1=A(42) (7FD1B79E9000) a2=null (null) a1=A(42) (7FD1B79E9000) a2=A(42) (7FD1B79E9000) b=B(12) b2=B(0) b=B(12) b2=B(12) "move" was supposed to initialize "source" to init. This does not appear to be the case. Is that a bug? Where?
Dec 05 2017
On Tuesday, 5 December 2017 at 14:11:02 UTC, Shachar Shemesh wrote:import std.algorithm: move; import std.stdio; import std.string; class A { int val; override string toString() const { return "A(%s)".format(val); } } struct B { int val; } void main() { B b = B(12); B* bp = &b; B* bp2; writefln("bp=%s bp2=%s", bp, bp2); move( bp, bp2 ); writefln("bp=%s bp2=%s", bp, bp2); A a1 = new A(); a1.val = 42; A a2; writefln("a1=%s (%s) a2=%s (%s)", a1, cast(void*)a1, a2, cast(void*)a2); move( a1, a2 ); writefln("a1=%s (%s) a2=%s (%s)", a1, cast(void*)a1, a2, cast(void*)a2); B b2; writefln("b=%s b2=%s", b, b2); move( b, b2 ); writefln("b=%s b2=%s", b, b2); } Answer: bp=7FFFAB559970 bp2=null bp=7FFFAB559970 bp2=7FFFAB559970 a1=A(42) (7FD1B79E9000) a2=null (null) a1=A(42) (7FD1B79E9000) a2=A(42) (7FD1B79E9000) b=B(12) b2=B(0) b=B(12) b2=B(12) "move" was supposed to initialize "source" to init. This does not appear to be the case. Is that a bug? Where?``` b=B(12) b2=B(0) b=B(12) b2=B(12) ``` bit will change if you declare destructor in your struct. As per the documentation:If T is a struct with a destructor or postblit defined, source is reset to its .init value after it is moved into target, otherwise it is left unchanged.as for others I understand you have the same problem here - per documentation, these are not structs with destructors, so moving the pointers (references) doesn't clear the source one.
Dec 05 2017
On 12/05/2017 06:11 AM, Shachar Shemesh wrote:"move" was supposed to initialize "source" to init. This does not appear to be the case. Is that a bug? Where?The same issue came up recently in the learn group regarding moveFront(). The documentation fails to mention that the .init behavior is only for hasElaborateCopyConstructor!(ElementType!R): http://forum.dlang.org/post/ovs6hq$1gfh$1 digitalmars.com Finally opened an issue about that one: https://issues.dlang.org/show_bug.cgi?id=18036 Ali
Dec 05 2017