ref is unsafe
Michel Fortin
michel.fortin at michelf.ca
Sun Dec 30 18:58:51 PST 2012
On 2012-12-30 22:29:33 +0000, Jonathan M Davis <jmdavisProg at gmx.com> said:
> Good point. Member variables of parameters also cause problems. So, it very
> quickly devolves to any function which accepts a user-defined type by ref and
> returns anything by ref would have to be @system, which is far from pleasant.
Note that the above definition includes every struct member function
that returns a ref because of the implicit this parameter.
Also, it's not just functions returning by ref, it could be a function
returning a delegate too, if the delegate happens to make use of the
reference:
void delegate() foo(ref int a) {
return { writeln(a); };
}
void delegate() bar() {
int a;
return foo(a); // leaking reference to a beyond bar's scope
}
And similar to passing a value by ref: you can pass a slice to a static
array, then return the slice:
int[] foo(int[] a) {
return a;
}
int[] bar() {
int[2] a;
return foo(a[]);
}
Three variations on the same theme.
--
Michel Fortin
michel.fortin at michelf.ca
http://michelf.ca/
More information about the Digitalmars-d
mailing list