ref is unsafe

Timon Gehr timon.gehr at gmx.ch
Wed Jan 2 21:56:26 PST 2013


On 01/03/2013 12:48 AM, Jason House wrote:
> ...
>
>>
>> ref int bar()
>> {
>>     int i = 7;
>>     return foo(i);
>> }
>
> If @safe, this code will not compile.
> Error: foo may return a local stack variable
> Since "i" is a local variable, "foo(i)" might return it.
>
>
>>
>> ref int baz(int i)
>> {
>>     return foo(i);
>> }
>
> This function is fine. "i" is an input argument so "foo(i)" is
> considered to be equivalent to an input argument.
>

Those two cases are pretty much the same.


More information about the Digitalmars-d mailing list