emplace

Constructs a new object of type T in memory with the given arguments.

If T is a $(D_KEYWORD class), emplace returns a class reference of type T, otherwise a pointer to the constructed object is returned.

If T is a nested class inside another class, outer should be an instance of the outer class.

args are arguments for the constructor of T. If T isn't an aggregate type and doesn't have a constructor, memory can be initialized to args[0] if Args.length == 1, Args[0] should be implicitly convertible to T then.

  1. T emplace(void[] memory, U outer, Args args)
  2. T emplace(void[] memory, Args args)
    T
    emplace
    (
    T
    Args...
    )
    (
    void[] memory
    ,
    auto ref Args args
    )
    if (
    is(T == class) &&
    &&
    )
    out (result) { }
  3. T* emplace(void[] memory, Args args)
  4. T* emplace(void[] memory, Args args)

Parameters

T

Constructed type.

Args

Types of the constructor arguments if T has a constructor or the type of the initial value.

args Args

Constructor arguments if T has a constructor or the initial value.

Return Value

Type: T

New instance of type T constructed in memory.

Precondition: memory.length == stateSize!T. Postcondition: memory and the result point to the same memory.

Examples

class C
{
    int i = 5;
    class Inner
    {
        int i;

        this(int param) pure nothrow @safe @nogc
        {
            this.i = param;
        }
    }
}
ubyte[stateSize!C] memory1;
ubyte[stateSize!(C.Inner)] memory2;

auto c = emplace!C(memory1);
assert(c.i == 5);

auto inner = emplace!(C.Inner)(memory2, c, 8);
assert(c.i == 5);
assert(inner.i == 8);
assert(inner.outer is c);

Meta