View source code
Display the source code in core/memory.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 core.memory.moveToGC

Moves a value to a new GC allocation.

T* moveToGC(T) (
  auto ref T value
);

Parameters

NameDescription
value Value to be moved. If the argument is an lvalue and a struct with a destructor or postblit, it will be reset to its .init value.

Returns

A pointer to the new GC-allocated value.

Example

struct S
{
    int x;
    this(this) @disable;
    ~this() @safe pure nothrow @nogc {}
}

S* p;

// rvalue
p = moveToGC(S(123));
writeln(p.x); // 123

// lvalue
auto lval = S(456);
p = moveToGC(lval);
writeln(p.x); // 456
writeln(lval.x); // 0

Authors

Sean Kelly, Alex Rønne Petersen

License

Boost License 1.0