英語版
このページの英語版を見る

core.lifetime

pure nothrow @safe T* emplace(T)(T* chunk);
初期化されていないメモリへの chunkとして型付けされている)。 Tclass 型でないT 型のオブジェクトを構築する。 アドレスに構築する。T がクラスの場合、クラス参照を NULL に初期化する。
Returns:
新しく構築されたオブジェクトへのポインタ(これは と同じである)。 chunk).
Examples:
static struct S
{
    int i = 42;
}
S[2] s2 = void;
emplace(&s2);
assert(s2[0].i == 42 && s2[1].i == 42);
Examples:
interface I {}
class K : I {}

K k = void;
emplace(&k);
assert(k is null);

I i = void;
emplace(&i);
assert(i is null);
T* emplace(T, Args...)(T* chunk, auto ref Args args)
if (is(T == struct) || Args.length == 1);
初期化されていないメモリー(しかしすでに型付けされている chunkへのポインタが与えられると、 で、 型のオブジェクトを構築する。 Tへのポインタが与えられると、引数からT 型のオブジェクトを構築する。 型のオブジェクトを構築する。 args.T がクラスの場合、クラス参照を へのクラス参照を初期化する。 args[0]. の対応するコンストラクタが@trusted の場合、この関数は となる。 T の対応するコンストラクタが@safe の場合、この関数は となる。
Returns:
新しく構築されたオブジェクトへのポインタ(これは である)。 chunk).
Examples:
int a;
int b = 42;
assert(*emplace!int(&a, b) == 42);
T emplace(T, Args...)(T chunk, auto ref Args args)
if (is(T == class));
生のメモリ領域が与えられた chunk(が与えられる(ただし、T のクラス型としてすでに型付けされている)、 は、そのアドレスにclassT のオブジェクトを構築する。コンストラクタ には引数Args が渡される。 T が内部クラスで、そのouter フィールドを使用して包含クラスのインスタンスにアクセスできる場合、 は、 型のオブジェクトをそのアドレスに構築する。 のインスタンスにアクセスできる内部クラスである場合、Args は空であってはならない。 の最初のメンバは、outer フィールドの有効なイニシャライザでなければならない。このフィールドの正しい初期化は このフィールドの正しい初期化は、T メソッド内で外部クラスのメンバにアクセスするために不可欠である。

注釈 T の対応するコンストラクタが@safe の場合、この関数は@safe となる。

Returns:
新しく構築されたオブジェクト。
Examples:
() @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);
    assert(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)));
}();
T emplace(T, Args...)(void[] chunk, auto ref Args args)
if (is(T == class));
生のメモリ領域 chunkが与えられると、class 型のオブジェクトT を構築する。 型のオブジェクトを構築する。コンストラクタには引数Args が渡される。 T が内部クラスで、outer フィールドがそのクラスのインスタンスにアクセスするために使用できる場合、 は空であってはならない。 のインスタンスにアクセスできる内部クラスである場合、Args は空であってはならない。 の最初のメンバは、outer フィールドの有効なイニシャライザでなければならない。このフィールドの正しい初期化は このフィールドの正しい初期化は、T メソッド内で外部クラスのメンバにアクセスするために不可欠である。

事前条件 chunkは、少なくともT が必要とするのと同じ大きさでなければならない。 Tの倍数でなければならない。(class インスタンスのサイズは _traits(classInstanceSize, T)).

注釈: T の対応するコンストラクタが@safe である場合、この関数は@trusted となる。

Returns:
新しく構築されたオブジェクト。
Examples:
static class C
{
    int i;
    this(int i){this.i = i;}
}
auto buf = new void[__traits(classInstanceSize, C)];
auto c = emplace!C(buf, 5);
assert(c.i == 5);
Examples:
// -betterCでも機能する:

static extern (C++) class C
{
    @nogc pure nothrow @safe:
    int i = 3;
    this(int i)
    {
        assert(this.i == 3);
        this.i = i;
    }
    int virtualGetI() { return i; }
}

align(__traits(classInstanceAlignment, C)) byte[__traits(classInstanceSize, C)] buffer;
C c = emplace!C(buffer[], 42);
assert(c.virtualGetI() == 42);
T* emplace(T, Args...)(void[] chunk, auto ref Args args)
if (!is(T == class));
生のメモリ領域 chunkが与えられると、そのアドレスに非classT のオブジェクトを構築する。コンストラクタには 引数 argsを渡される。

事前条件 chunkは少なくとも T でなければならず、T のアライメントの倍数でなければならない。 のアラインメントの倍数でなければならない。

注釈:この関数は、。 の対応するコンストラクタが@trustedT の対応するコンストラクタが@safe である場合、この関数は となる。

Returns:
新しく構築されたオブジェクトへのポインタ。
Examples:
struct S
{
    int a, b;
}
void[S.sizeof] buf = void;
S s;
s.a = 42;
s.b = 43;
auto s1 = emplace!S(buf, s);
assert(s1.a == 42 && s1.b == 43);
@system void copyEmplace(S, T)(ref S source, ref T target)
if (is(immutable(S) == immutable(T)));
指定されたソース値のコピーを未初期化メモリに配置する、 つまり T target = sourceコピーをシミュレートする。 つまり、ターゲットメモリがすでに割り当てられていて、コピーで初期化される場合のコピー構築をシミュレートする。
Parameters:
S source ターゲットにコピーされる値
T target コピー元のコピーで初期化される未初期化値
Examples:
int source = 123;
int target = void;
copyEmplace(source, target);
assert(target == 123);
Examples:
immutable int[1][1] source = [ [123] ];
immutable int[1][1] target = void;
copyEmplace(source, target);
assert(target[0][0] == 123);
Examples:
struct S
{
    int x;
    void opAssign(const scope ref S rhs) @safe pure nothrow @nogc
    {
        assert(0);
    }
}

S source = S(42);
S target = void;
copyEmplace(source, target);
assert(target.x == 42);
template forward(args...)
outreflazy をパラメータに保持したまま、関数の引数を転送する。 を維持したまま関数の引数を転送する。
Parameters:
args パラメータ・リストまたは std.meta.AliasSeq.
Returns:
argsAliasSeq で、outreflazy が保存されている。
Examples:
class C
{
    static int foo(int n) { return 1; }
    static int foo(ref int n) { return 2; }
}

// 前方
int bar()(auto ref int x) { return C.foo(forward!x); }

// フォワードなし
int baz()(auto ref int x) { return C.foo(x); }

int i;
assert(bar(1) == 1);
assert(bar(i) == 2);

assert(baz(1) == 2);
assert(baz(i) == 2);
Examples:
void foo(int n, ref string s) { s = null; foreach (i; 0 .. n) s ~= "Hello"; }

// パラメータ・タプルにバインドされているすべての引数を転送する
void bar(Args...)(auto ref Args args) { return foo(forward!args); }

// すべての引数を順序を入れ替えて転送する
void baz(Args...)(auto ref Args args) { return foo(forward!args[$/2..$], forward!args[0..$/2]); }

string s;
bar(1, s);
assert(s == "Hello");
baz(s, 2);
assert(s == "HelloHello");
Examples:
struct X {
    int i;
    this(this)
    {
        ++i;
    }
}

struct Y
{
    private X x_;
    this()(auto ref X x)
    {
        x_ = forward!x;
    }
}

struct Z
{
    private const X x_;
    this()(auto ref X x)
    {
        x_ = forward!x;
    }
    this()(auto const ref X x)
    {
        x_ = forward!x;
    }
}

X x;
const X cx;
auto constX = (){ const X x; return x; };
static assert(__traits(compiles, { Y y = x; }));
static assert(__traits(compiles, { Y y = X(); }));
static assert(!__traits(compiles, { Y y = cx; }));
static assert(!__traits(compiles, { Y y = constX(); }));
static assert(__traits(compiles, { Z z = x; }));
static assert(__traits(compiles, { Z z = X(); }));
static assert(__traits(compiles, { Z z = cx; }));
static assert(__traits(compiles, { Z z = constX(); }));


Y y1 = x;
// ref lvalue, copy
assert(y1.x_.i == 1);
Y y2 = X();
// r値, 移動
assert(y2.x_.i == 0);

Z z1 = x;
// ref lvalue, copy
assert(z1.x_.i == 1);
Z z2 = X();
// r値, 移動
assert(z2.x_.i == 0);
Z z3 = cx;
// ref const lvalue, copy
assert(z3.x_.i == 1);
Z z4 = constX();
// const rvalue, copy
assert(z4.x_.i == 1);
void move(T)(ref T source, ref T target);

T move(T)(ref return scope T source);
移動する sourceに移動する。 targetに移動する。
T がデストラクタまたはポストブリットが定義された構造体である場合、ソースはターゲットに移動された後、 の値にリセットされる。 .init の値はリセットされる。 は変更されない。

前提条件 ソースが自身を指す内部ポインタを持ち、かつ を定義していない場合、移動できず、アサーション失敗のトリガーとなる。

Parameters:
T source コピーするデータ
T target どこにコピーするか。デストラクタがあれば、コピーが実行される前に呼び出される。 が呼び出される。
Examples:
非構造体型では moveを実行するだけである。 target = source:
Object obj1 = new Object;
Object obj2 = obj1;
Object obj3;

move(obj2, obj3);
assert(obj3 is obj1);
// obj2は変更されない
assert(obj2 is obj1);
Examples:
// デストラクタのない構造体は、単にコピーされる
struct S1
{
    int a = 1;
    int b = 2;
}
S1 s11 = { 10, 11 };
S1 s12;

move(s11, s12);

assert(s12 == S1(10, 11));
assert(s11 == s12);

// しかし、デストラクタやポストブリットを持つ構造体は、ターゲットにコピーされた後、
// その.init値にリセットされる。
struct S2
{
    int a = 1;
    int b = 2;

    ~this() pure nothrow @safe @nogc { }
}
S2 s21 = { 3, 4 };
S2 s22;

move(s21, s22);

assert(s21 == S2(1, 2));
assert(s22 == S2(3, 4));
Examples:
コピー不可能な構造体も移動できる:
struct S
{
    int a = 1;
    @disable this(this);
    ~this() pure nothrow @safe @nogc {}
}
S s1;
s1.a = 2;
S s2 = move(s1);
assert(s1.a == 1);
assert(s2.a == 2);
@system void moveEmplace(T)(ref T source, ref T target);
と似ている。 moveに似ているが targetは初期化されていない。これは の方が効率的である。 sourceこの方が効率的である。 target これはより効率的である。
Parameters:
T source ターゲットに移される値
T target ソースが埋める未初期化の値
Examples:
static struct Foo
{
pure nothrow @nogc:
    this(int* ptr) { _ptr = ptr; }
    ~this() { if (_ptr) ++*_ptr; }
    int* _ptr;
}

int val;
Foo foo1 = void; // 未初期化
auto foo2 = Foo(&val); // 初期化
assert(foo2._ptr is &val);

// `move(foo2,foo1)`を使うと、初期化されていないfoo1を壊してしまうので、
// 未定義の効果がある。
// moveEmplaceはfoo1を破壊したり初期化したりすることなく、直接上書きする。
moveEmplace(foo2, foo1);
assert(foo1._ptr is &val);
assert(foo2._ptr is null);
assert(val == 0);
template _d_delstructImpl(T)
_d_delstruct_d_delstructTrace
pure nothrow @nogc @trusted void _d_delstruct(ref T p);
これは、削除される値がデストラクタを持つ構造体へのポインタであるが、オーバーロードされていない デストラクタを持つ構造体へのポインタであるが、オーバーロードされた演算子を持たない delete 演算子を持たない場合に呼び出される。
Parameters:
T p 削除される値へのポインタ。
Bugs:
この"関数テンプレート化された関数"は、安全性チェックと純粋性チェックとスローアビリティ・チェックをバイパスする、かなり古いランタイム・フックから移植されたものである。 を移植したものである。既存のコードを壊さないために 既存のコードを壊さないために、この関数テンプレートは一時的に宣言されている。 @trusted この関数テンプレートは一時的に宣言されている。 を期待する。
alias _d_delstructTrace = _d_HookTraceImpl!(T, _d_delstruct, errorMessage);
Bugs:
このテンプレート化された関数は、安全性チェックと純粋性チェックとスローアビリティ・チェックをバイパスする、かなり古いランタイム・フックから移植されたものである。 安全性、純粋性、投げやすさのチェックをバイパスしている。既存のコードを壊さないために 既存のコードを壊さないために、この関数テンプレートは一時的に宣言されている。 @trusted この関数テンプレートは一時的に宣言されている。 を期待する。
@trusted T _d_newThrowable(T)()
if (is(T : Throwable) && (__traits(getLinkage, T) == "D"));
例外プールからT 型の例外を割り当てる。 T Throwable 、またはそこから派生したものでなければならず、COMやC++クラスであってはならない。

注釈: この関数はコンストラクタを呼び出さない。 この関数はT のコンストラクタを呼び出さない。 forward!args を必要とし、-dip1008 でエラーを引き起こすからである。この不都合は を必要とするためである。

Returns:
型のインスタンスを割り当てる。T
@trusted T _d_newclassT(T)()
if (is(T == class));
新しいクラスのインスタンスを作成する。 メモリを確保し、フィールドに初期値を設定するが、コンストラクタは呼び出さない。 コンストラクタは呼び出さない。
new C() // _d_newclass!(C)()
Returns:
新しく生成されたオブジェクト
@trusted T _d_newclassTTrace(T)(string file, int line, string funcname);
TraceGCラッパー core.lifetime.d_newclassT.
@trusted T* _d_newitemT(T)();
初期化された非配列項目を割り当てる。
これは、_arrayPad(size)のような配列に必要なものを避けるための最適化である。 ヒープ上に構造体のインスタンスを割り当てる。
struct Sz {int x = 0;}
struct Si {int x = 3;}

void main()
{
    new Sz(); // ゼロ初期化を使う
    new Si(); // Si.initを使用する
}
Returns:
新たに割り当てられた項目