View source code
Display the source code in std/algorithm/mutation.d from which this page was generated on github.
Report a bug
If you spot a problem with this page, click here to create a Bugzilla issue.
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.

Function std.algorithm.mutation.moveEmplace

Similar to move but assumes target is uninitialized. This is more efficient because source can be blitted over target without destroying or initializing it first.

void moveEmplace(T) (
  ref T source,
  ref T target
) pure @system;

Parameters

NameDescription
source value to be moved into target
target uninitialized value to be filled by source

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
assert(foo2._ptr is &val);

// Using `move(foo2, foo1)` would have an undefined effect because it would destroy
// the uninitialized foo1.
// moveEmplace directly overwrites foo1 without destroying or initializing it first.
moveEmplace(foo2, foo1);
assert(foo1._ptr is &val);
assert(foo2._ptr is null);
writeln(val); // 0

Authors

Andrei Alexandrescu

License

Boost License 1.0.