Function IntrusivePtr.alloc
Constructs an object of type ElementType
and wraps it in a IntrusivePtr
using args as the parameter list for the constructor of ElementType
.
IntrusivePtr!ElementType alloc(bool supportGC = platformSupportGC, AllocatorType, Args...)
(
AllocatorType a,
auto ref Args args
);
The object is constructed as if by the expression emplace!ElementType(_payload, forward!args)
, where payload is an internal pointer to storage suitable to hold an object of type ElementType
.
The storage is typically larger than ElementType
in order to use one allocation for both the control block and the ElementType
object.
Examples
static struct Foo{
ControlBlock!(int, int) c;
int i;
this(int i)pure nothrow @safe @nogc{
this .i = i;
}
}
{
import std .experimental .allocator : allocatorObject;
auto a = allocatorObject(Mallocator .instance);
{
auto x = IntrusivePtr!Foo .alloc(a);
assert(x .get .i == 0);
auto y = IntrusivePtr!(const Foo) .alloc(a, 2);
assert(y .get .i == 2);
}
{
static struct Struct{
ControlBlock!(int) c;
int i = 7;
this(int i)pure nothrow @safe @nogc{
this .i = i;
}
}
auto s1 = IntrusivePtr!Struct .alloc(a);
assert(s1 .get .i == 7);
auto s2 = IntrusivePtr!Struct .alloc(a, 123);
assert(s2 .get .i == 123);
}
}