D 为什么从结构数组返回时不调用this(this)?
D2.058中的上述内容给出了: 在这个(这个) 在~this():localString中 In~this():defaultString 在~this():H.m_ 在~this():H.m_D 为什么从结构数组返回时不调用this(this)?,d,D,D2.058中的上述内容给出了: 在这个(这个) 在~this():localString中 In~this():defaultString 在~this():H.m_ 在~this():H.m_ 在上述过程中(从getSMem()调用)只生成一个this(this)。getSLocal()调用只能移动结构。但是,为什么getSVec()不会导致this(this)?我注意到这是std.container.Array中保存的引用计数结构的上下文,与~this()相比,对此(this)的调用太少了
在上述过程中(从getSMem()调用)只生成一个this(this)。getSLocal()调用只能移动结构。但是,为什么getSVec()不会导致this(this)?我注意到这是std.container.Array中保存的引用计数结构的上下文,与~this()相比,对此(this)的调用太少了。在
getSVec
的情况下,它看起来像是一个bug,可能与有关,尽管情况并不完全相同。不过,您应该报告您的具体示例,因为它可能不是完全相同的bug
但是,正如您所说,在
getSLocal
的情况下,局部变量会被移动,因此不会发生复制,也不需要postblit调用。只是getSVec
有bug。谢谢,我刚刚在以下位置注册了此bug:
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();
}