std.conv.emplace - multiple declarations
Function emplace
Given a pointer chunk to uninitialized memory (but already typed
as T), constructs an object of non-class type T at that
address. If T is a class, initializes the class reference to null.
T* emplace(T)
(
T* chunk
) pure nothrow @safe;
Returns
A pointer to the newly constructed object (which is the same
as chunk).
Example
static struct S
{
int i = 42;
}
S[2] s2 = void;
emplace(&s2);
assert(s2[0] .i == 42 && s2[1] .i == 42);
Example
interface I {}
class K : I {}
K k = void;
emplace(&k);
assert(k is null);
I i = void;
emplace(&i);
assert(i is null);
Function emplace
Given a pointer chunk to uninitialized memory (but already typed
as a non-class type T), constructs an object of type T at
that address from arguments args. If T is a class, initializes
the class reference to args[0].
T* emplace(T, Args...)
(
T* chunk,
auto ref Args args
)
if (is(T == struct) || Args .length == 1);
This function can be @trusted if the corresponding constructor of
T is @safe.
Returns
A pointer to the newly constructed object (which is the same
as chunk).
Example
int a;
int b = 42;
writeln(*emplace!int(&a, b)); // 42
Function emplace
Given a raw memory area chunk (but already typed as a class type T),
constructs an object of class type T at that address. The constructor
is passed the arguments Args.
T emplace(T, Args...)
(
T chunk,
auto ref Args args
)
if (is(T == class));
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.
Note
This function is @safe if the corresponding constructor of T is @safe.
Returns
The newly constructed object.
Example
() @safe {
class SafeClass
{
int x;
@safe this(int x) { this .x = x; }
}
auto buf = new void[__traits(classInstanceSize, SafeClass)];
auto support = (() @trusted => cast(SafeClass)(buf .ptr))();
auto safeClass = emplace!SafeClass(support, 5);
writeln(safeClass .x); // 5
class UnsafeClass
{
int x;
@system this(int x) { this .x = x; }
}
auto buf2 = new void[__traits(classInstanceSize, UnsafeClass)];
auto support2 = (() @trusted => cast(UnsafeClass)(buf2 .ptr))();
static assert(!__traits(compiles, emplace!UnsafeClass(support2, 5)));
static assert(!__traits(compiles, emplace!UnsafeClass(buf2, 5)));
}();
Function emplace
Given a raw memory area chunk, constructs an object of class type T at
that address. The constructor is passed the arguments Args.
T emplace(T, Args...)
(
void[] chunk,
auto ref Args args
)
if (is(T == class));
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.
Returns
The newly constructed object.
Example
static class C
{
int i;
this(int i){this .i = i;}
}
auto buf = new void[__traits(classInstanceSize, C)];
auto c = emplace!C(buf, 5);
writeln(c .i); // 5
Function emplace
Given a raw memory area chunk, constructs an object of non-class type T at that address. The constructor is passed the
arguments args, if any.
T* emplace(T, Args...)
(
void[] chunk,
auto ref Args args
)
if (!is(T == class));
Preconditions
chunk must be at least as large
as T needs and should have an alignment multiple of T's
alignment.
Note
This function can be @trusted if the corresponding constructor of
T is @safe.
Returns
A pointer to the newly constructed object.
Example
struct S
{
int a, b;
}
auto buf = new void[S .sizeof];
S s;
s .a = 42;
s .b = 43;
auto s1 = emplace!S(buf, s);
assert(s1 .a == 42 && s1 .b == 43);
Authors
Walter Bright, Andrei Alexandrescu, Shin Fujishiro, Adam D. Ruppe, Kenji Hara