What does the following program do?
Nemanja Boric
4burgos at gmail.com
Tue Dec 5 14:18:23 UTC 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.
More information about the Digitalmars-d
mailing list