Report a bug
If you spot a problem with this page, click here to create a GitHub issue.
Improve this page
Quickly fork, edit online, and submit a pull request for this page. Requires a signed-in GitHub account. This works well for small changes. If you'd like to make larger changes you may want to consider using a local clone.

mir.conv

Conversion utilities.
License:
Authors:
Ilia Ki
template to(T)
The to template converts a value from one type to another. The source type is deduced and the target type must be specified, for example the expression to!int(42.0) converts the number 42 from double to int. The conversion is "unsafe", i.e., it does not check for overflow.
Examples:
enum E
{
    A,
    B,
    C,
}

assert(to!E("B") == E.B);
assert(to!string(E.B) == "B");
assert(to!string(null) is null);
assert(to!string(true) == "true");
assert(to!string(false) == "false");

enum S : wstring
{
    a = "A",
    b = "B",
}

assert(to!wstring(S.b) == "B"w);
assert(to!S("B"w) == S.b);
ref T to(A...)(auto ref A args)
if (A.length > 0);
pure nothrow @trusted void emplaceInitializer(T)(ref scope T chunk);
Emplace helper function.
nothrow @nogc T[] uninitializedFillDefault(T)(return scope T[] array);
Examples:
static struct S { int x = 42; @disable this(this); }

int[5] expected = [42, 42, 42, 42, 42];
S[5] arr = void;
uninitializedFillDefault(arr);
assert((cast(int*) arr.ptr)[0 .. arr.length] == expected);
Examples:
int[] a = [1, 2, 4];
uninitializedFillDefault(a);
assert(a == [0, 0, 0]);
void xdestroy(T)(scope T[] ar);
Destroy structs and unions usnig __xdtor member if any. Do nothing for other types.
Examples:
__gshared int d;
__gshared int c;
struct D { ~this() nothrow @nogc {d++;} }
extern(C++)
struct C { ~this() nothrow @nogc {c++;} }
C[2] carray;
D[2] darray;
carray.xdestroy;
darray.xdestroy;
assert(c == 2);
assert(d == 2);
c = 0;
d = 0;