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)
  3. T* emplace(void[] memory, Args args)
  4. T* emplace(void[] memory, Args args)
    T*
    emplace
    (
    T
    Args...
    )
    (
    void[] memory
    ,
    auto ref Args args
    )
    out (result) { assert (memory.ptr is result); }
  5. void initializeOne(void[] memory, T* result)

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

ubyte[4] memory;

auto i = emplace!int(memory);
static assert(is(typeof(i) == int*));
assert(*i == 0);

i = emplace!int(memory, 5);
assert(*i == 5);

static struct S
{
    int i;
    @disable this();
    @disable this(this);
    this(int i) @nogc nothrow pure @safe
    {
        this.i = i;
    }
}
auto s = emplace!S(memory, 8);
static assert(is(typeof(s) == S*));
assert(s.i == 8);

Meta