move

Moves source into target, via a destructive copy when necessary.

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.

Preconditions: If source has internal pointers that point to itself, it cannot be moved, and will trigger an assertion failure.

  1. void move(ref T source, ref T target)
  2. T move(return scope ref T source)
    T
    move
    (
    T
    )
    (
    return scope ref T source
    )

Parameters

source
Type: T

Data to copy.

Examples

For non-struct types, move just performs target = source:

1 Object obj1 = new Object;
2 Object obj2 = obj1;
3 Object obj3;
4 
5 move(obj2, obj3);
6 assert(obj3 is obj1);
7 // obj2 unchanged
8 assert(obj2 is obj1);
1 // Structs without destructors are simply copied
2 struct S1
3 {
4     int a = 1;
5     int b = 2;
6 }
7 S1 s11 = { 10, 11 };
8 S1 s12;
9 
10 move(s11, s12);
11 
12 assert(s12 == S1(10, 11));
13 assert(s11 == s12);
14 
15 // But structs with destructors or postblits are reset to their .init value
16 // after copying to the target.
17 struct S2
18 {
19     int a = 1;
20     int b = 2;
21 
22     ~this() pure nothrow @safe @nogc { }
23 }
24 S2 s21 = { 3, 4 };
25 S2 s22;
26 
27 move(s21, s22);
28 
29 assert(s21 == S2(1, 2));
30 assert(s22 == S2(3, 4));

Non-copyable structs can still be moved:

1 struct S
2 {
3     int a = 1;
4     @disable this(this);
5     ~this() pure nothrow @safe @nogc {}
6 }
7 S s1;
8 s1.a = 2;
9 S s2 = move(s1);
10 assert(s1.a == 1);
11 assert(s2.a == 2);
Suggestion Box / Bug Report