ref is unsafe

Jonathan M Davis jmdavisProg at gmx.com
Sun Dec 30 01:17:37 PST 2012


On Sunday, December 30, 2012 10:04:01 Daniel Kozak wrote:
> IMHO, try to return ref to local variable should be error, and
> such a code shouldn't be compilable

You can disallow that in the easy case of

ref int boo(int i)
{
    return i;
}

and in fact, that's already illegal. The problem is the wrapper function. 
You'd also have to disallow functions from returning ref parameters by ref. 
Otherwise,

ref int foo(ref int i)
{
    return i;
}

ref int baz(int i)
{
    return foo(i);
}

continues to cause problems. And making it illegal to return ref parameters by 
ref would be a serious problem for wrapper ranges, because they do that sort 
of thing all the time with front. So, that's not really going to work.

- Jonathan M Davis


More information about the Digitalmars-d mailing list