View source code
Display the source code in std/algorithm/mutation.d from which this page was generated on
github.
Improve this page
Quickly fork, edit online, and submit a pull request for this page.
Requires a signed-in GitHub account. This works well for small changes.
If you'd like to make larger changes you may want to consider using
local clone.
Page wiki
View or edit the community-maintained wiki page associated with this page.
Function std.algorithm.mutation.move
Moves
into source
via a destructive target
copy
.
Prototypes
void move(T)( T source, T target ); T move(T)( T source );
Parameters
Name | Description |
---|---|
source | Data to copy . If a destructor or postblit is defined, it is reset
to its .init value after it is moved into target . Note that data
with internal pointers that point to itself cannot be moved, and will
trigger an assertion failure. |
target | Where to copy into. The destructor, if any, is invoked before the
copy is performed. |
Example
Object obj1 = new Object; Object obj2 = obj1; Object obj3; move(obj2, obj3); assert(obj3 is obj1);
Example
// Structs without destructors are simply copied struct S1 { int a = 1; int b = 2; } S1 s11 = { 10, 11 }; S1 s12; move(s11, s12); assert(s11.a == 10 && s11.b == 11 && s12.a == 10 && s12.b == 11); // But structs with destructors or postblits are reset to their .init value // after copying to the target. struct S2 { int a = 1; int b = 2; ~this() pure nothrow @safe @nogc { } } S2 s21 = { 3, 4 }; S2 s22; move(s21, s22); assert(s21.a == 1 && s21.b == 2 && s22.a == 3 && s22.b == 4);
Example
struct S { @disable this(this); ~this() pure nothrow @safe @nogc {} } S s1; S s2 = move(s1);
Example
static struct Foo
{
pure nothrow @nogc:
this(int* ptr) { _ptr = ptr; }
~this() { if (_ptr) ++*_ptr; }
int* _ptr;
}
int val;
Foo foo1 = void; // uninitialized
auto foo2 = Foo(&val); // initialized
// Using move(foo2, foo1)
has an undefined effect because it destroys the uninitialized foo1.
// MoveEmplace directly overwrites foo1 without destroying or initializing it first.
assert(foo2._ptr is &val);
moveEmplace(foo2, foo1);
assert(foo1._ptr is &val && foo2._ptr is null);