escaping addresses of ref parameters - not

Andrei Alexandrescu SeeWebsiteForEmail at erdani.org
Mon Feb 9 07:52:28 PST 2009


Steven Schveighoffer wrote:
> "Andrei Alexandrescu" wrote
>> The compiler's escape detection mechanism can't help quite a lot here 
>> because the escape hatch is rather indirect.
>>
>> Initially I thought SafeD should prevent such escapes, whereas D allows 
>> them. Now I start thinking the pattern above is dangerous enough to be 
>> disallowed in all of D. How about this rule?
>>
>> ***************
>> Rule: ref parameters are PASS-DOWN and RETURN only. No escaping of 
>> addresses of ref parameters is allowed. If you want to escape the address 
>> of a ref parameter, use a pointer in the first place.
>> ***************
> 
> As long as there is a way to circumvent this, I'm OK with this rule. 
> Something that's the equivalent of a cast.  Two reasons:
> 
> 1. Using a  * dereference pointer inside a function for all usages is 
> sometimes tedious.  This would be a non issue if ref local variables were 
> allowed, i.e.:
> 
> void foo(int *x)
> {
>   ref int rx = *x;
>   // use rx until you need to copy the address of x.
> }
> 
> 2. you may need to call functions you have no control over that take a 
> pointer but do not save a reference to it.  e.g. system calls.
> 
> -Steve 
> 
> 

Yah, an explicit cast ref T -> T* must be still allowed.

Andrei



More information about the Digitalmars-d mailing list