Why is this (this) not called when returning from an array of structures?

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(); } 

The above in D2.058 gives:

In this (this)

In ~ this (): localString

In ~ this (): defaultString

In ~ this (): H.m_s

In ~ this (): H.m_s

Only this one (this) is created in the above (from the call to getSMem ()). A call to getSLocal () can simply move the structure. However, why does not getSVec () lead to this (this)? I noticed that this is the context of the reference count structure stored in std.container.Array, and there were too few calls for this (this) compared to ~ this ().

+4
source share
1 answer

In the case of getSVec it looks like an error, possibly related to this , although the situation is not quite the same, you should provide your specific example, although it may not be the same error.

However, as you say, in the case of getSLocal local variable moves, so copying does not occur, and no subsequent call is required. This is just getSVec , which is a mistake.

+2
source

Source: https://habr.com/ru/post/1397231/


All Articles