Const ref and rvalues again...

Malte Skarupke malteskarupke at web.de
Sat Oct 20 11:25:24 PDT 2012


On Friday, 19 October 2012 at 13:00:52 UTC, Timon Gehr wrote:
> On 10/19/2012 09:53 AM, Jacob Carlborg wrote:
>> On 2012-10-19 04:48, Timon Gehr wrote:
>>
>>> Then how to specify that the value of x cannot be escaped?
>>> I'm in favour of doing it the other way round and disallow 
>>> escaping of
>>> ref parameters without an unsafe cast.
>>
>> "scope" is supposed to be used to prevent this.
>>
>
> Sure, but how?
>
> void goo(scope int* x){
>     global0 = x; // should clearly be disallowed
> }
>
> void foo(scope ref int*** x){
>     global1 = &x; // ?
>     global2 = x;  // ?
>     global3 = *x; // ?
>     globall4 = **x; // ?
> }
>
> Maybe we need this:
>
> void foo(scope ref int*** x);   // ?
> void foo(ref int(***)scope  x); // no escaping of x, *x, **x
> void foo(ref int*(**)scope x);  // may escape **x
> void foo(ref int**(*)scope x);  // may escape *x, **x
>
> What about &x?

No scope should mean that you can not escape the address of 
something. So

void goo(scope int* x)
{
     global0 = x;
}

Should be allowed. Scope in this case applies to the pointer. Not 
to the thing it's pointing to. The scope keyword is not 
transitive. That wouldn't make sense. It is perfectly legal to 
have a scoped pointer to something allocated on the heap.


Whereas

void goo(scope ref int x)
{
     global0 = &x;
}

Should NOT be allowed. It looks like it's the same code, but in 
this case x is an integer. In the last example x was a pointer to 
an integer.

So it comes down to this:

void goo(scope int* x)
{
     global0 = x; // copying is allowed
     //global1 = &x; // referencing is not allowed
}




More information about the Digitalmars-d mailing list