SharedPtr.alloc

Constructs an object of type ElementType and wraps it in a SharedPtr using args as the parameter list for the constructor of ElementType.

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.sizeof in order to use one allocation for both the control block and the ElementType object.

  1. auto alloc(AllocatorType a, Args args)
    struct SharedPtr
    static
    alloc
    (
    bool supportGC = platformSupportGC
    AllocatorType
    Args...
    )
    (
    AllocatorType a
    ,
    auto ref Args args
    )
    if (
    !isDynamicArray!ElementType
    )
  2. auto alloc(AllocatorType allocator, ElementReferenceType element, DeleterType deleter)
  3. auto alloc(AllocatorType a, size_t n, Args args)

Examples

auto a = allocatorObject(Mallocator.instance);
{
	auto a = SharedPtr!long.alloc(a);
	assert(a.get == 0);

	auto b = SharedPtr!(const long).alloc(a, 2);
	assert(b.get == 2);
}

{
	static struct Struct{
		int i = 7;

		this(int i)pure nothrow @safe @nogc{
			this.i = i;
		}
	}

	auto s1 = SharedPtr!Struct.alloc(a);
	assert(s1.get.i == 7);

	auto s2 = SharedPtr!Struct.alloc(a, 123);
	assert(s2.get.i == 123);
}

{
	static interface Interface{
	}
	static class Class : Interface{
		int i;

		this(int i)pure nothrow @safe @nogc{
			this.i = i;
		}
	}

	SharedPtr!Interface x = SharedPtr!Class.alloc(a, 3);
	//assert(x.dynTo!Class.get.i == 3);
}

Meta