import std.stdio;
struct S
{
string m_str = "defaultString";
this(this)
{
writeln("In this(this)");
}
~this()
{
writeln("In ~this():"~m_str);
}
}
struct Holder
{
S[] m_arr;
S m_s;
this(S[] arr)
{
m_arr = arr;
m_s.m_str="H.m_s";
}
S getSMem()
{
return m_s;
}
S getSVec()
{
return m_arr[0];
}
S getSLocal()
{
S local = S("localString");
return local;
}
}
void main()
{
Holder h = Holder(new S[1]);
S s1 = h.getSMem();
S s2 = h.getSVec();
S s3 = h.getSLocal();
}
Вышеуказанное в D2.058 дает:
In this (this)
In ~ this (): localString
In ~ this(): defaultString
В ~ this (): H.m_s
В ~ this (): H.m_s
Только одно это (это)производится выше (из вызова getSMem ()).Вызов getSLocal () может просто переместить структуру.Однако почему getSVec () не приводит к this (this)?Я заметил, что это контекст структуры подсчета ссылок, хранящейся в std.container.Array, и было слишком мало вызовов this (this) по сравнению с ~ this ().