emplace

Given a raw memory area chunk, constructs an object of class type T at that address. The constructor is passed the arguments Args.

If T is an inner class whose outer field can be used to access an instance of the enclosing class, then Args must not be empty, and the first member of it must be a valid initializer for that outer field. Correct initialization of this field is essential to access members of the outer class inside T methods.

Preconditions: chunk must be at least as large as T needs and should have an alignment multiple of T's alignment. (The size of a class instance is obtained by using __traits(classInstanceSize, T)).

Note: This function can be @trusted if the corresponding constructor of T is @safe.

  1. T* emplace(T* chunk)
  2. T* emplace(T* chunk, Args args)
  3. T emplace(T chunk, Args args)
  4. T emplace(void[] chunk, Args args)
    T
    emplace
    (
    T
    Args...
    )
    (
    void[] chunk
    ,
    auto ref Args args
    )
    if (
    is(T == class)
    )
  5. T* emplace(void[] chunk, Args args)

Return Value

Type: T

The newly constructed object.

Examples

static class C
{
    int i;
    this(int i){this.i = i;}
}
auto buf = new void[__traits(classInstanceSize, C)];
auto c = emplace!C(buf, 5);
assert(c.i == 5);

Meta

Suggestion Box / Bug Report