make

Dynamically allocates (using alloc) and then creates in the memory allocated an object of type T, using args (if any) for its initialization. Initialization occurs in the memory allocated and is otherwise semantically the same as T(args). (Note that using alloc.make!(T[]) creates a pointer to an (empty) array of Ts, not an array. To use an allocator to allocate and initialize an array, use alloc.makeArray!T described below.)

make
(
T
Allocator
A...
)
(
auto ref Allocator alloc
,
auto ref A args
)

Parameters

T

Type of the object being created.

alloc Allocator

The allocator used for getting the needed memory. It may be an object implementing the static interface for allocators, or an IAllocator reference.

args A

Optional arguments used for initializing the created object. If not present, the object is default constructed.

Return Value

Type: auto

If T is a class type, returns a reference to the created T object. Otherwise, returns a T* pointing to the created object. In all cases, returns null if allocation failed.

Throws

If T's constructor throws, deallocates the allocated memory and propagates the exception.

Examples

1 // Dynamically allocate one integer
2 const int* p1 = theAllocator.make!int;
3 // It's implicitly initialized with its .init value
4 assert(*p1 == 0);
5 // Dynamically allocate one double, initialize to 42.5
6 const double* p2 = theAllocator.make!double(42.5);
7 assert(*p2 == 42.5);
8 
9 // Dynamically allocate a struct
10 static struct Point
11 {
12     int x, y, z;
13 }
14 // Use the generated constructor taking field values in order
15 const Point* p = theAllocator.make!Point(1, 2);
16 assert(p.x == 1 && p.y == 2 && p.z == 0);
17 
18 // Dynamically allocate a class object
19 static class Customer
20 {
21     uint id = uint.max;
22     this() {}
23     this(uint id) { this.id = id; }
24     // ...
25 }
26 Customer cust = theAllocator.make!Customer;
27 assert(cust.id == uint.max); // default initialized
28 cust = theAllocator.make!Customer(42);
29 assert(cust.id == 42);
30 
31 // explicit passing of outer pointer
32 static class Outer
33 {
34     int x = 3;
35     class Inner
36     {
37         auto getX() { return x; }
38     }
39 }
40 auto outer = theAllocator.make!Outer();
41 auto inner = theAllocator.make!(Outer.Inner)(outer);
42 assert(outer.x == inner.getX);

Meta

Suggestion Box / Bug Report