On Friday, 10 April 2015 at 21:50:13 UTC, Michel Fortin wrote:
On 2015-04-10 21:29:19 +0000, Walter Bright
<newshou...@digitalmars.com> said:
On 4/10/2015 2:11 PM, Martin Nowak wrote:
On 04/09/2015 01:10 AM, Walter Bright wrote:
http://wiki.dlang.org/DIP77
In the first problem example:
struct S {
RCArray!T array;
}
void main() {
auto s = S(RCArray!T([T()])); // s.array's refcount is
now 1
foo(s, s.array[0]); // pass by ref
}
void foo(ref S s, ref T t) {
s.array = RCArray!T([]); // drop the old s.array
t.doSomething(); // oops, t is gone
}
What do you do to pin s.array?
auto tmp = s;
or
auto tmp = s.array;
The latter.
And how is it pinned in this case?
struct S {
private RCArray!T array;
ref T opIndex(int index) return { return array[index]; }
void clear() { s.array = RCArray!T([]); }
}
void main() {
auto s = S(RCArray!T([T()])); // s.array's refcount is now
1
foo(s, s[0]); // pass by ref
}
void foo(ref S s, ref T t) {
s.clear(); // drop the old s.array
t.doSomething(); // oops, t is gone
}
AFAICT it would create a temporary copy of s.array, which would
keep t valid.
Nope. It'll scan at compile time the types reachable through
the parameters. If any of those types are RCO's that have a
type that matches a ref parameter, the
ref parameter's RCO will get copied.