C `restrict` keyword in D

Moritz Maxeiner via Digitalmars-d digitalmars-d at puremagic.com
Sat Sep 2 20:49:21 PDT 2017


On Sunday, 3 September 2017 at 03:04:58 UTC, Uknown wrote:
> [...]
>
> void foo(ref RCArray!int arr, ref int val) @safe
> {
>     {
> 	auto copy = arr; //arr's (and copy's) reference counts are 
> both 2
> 	arr = RCArray!int([]); // There is another owner, so arr
> 			       // forgets about the old payload
>     } // Last owner of the array ('copy') gets destroyed and 
> happily
>       // frees the payload.
>     val = 3; // Oops.
> }
>
> Here, adding `restrict` to foo's parameters like so :
>
> void foo(restrict ref RCArray!int arr, restrict ref int val)
>
> would make the compiler statically enforce the fact that 
> neither references are pointing to the same data. This would 
> cause an error in main, since arr[0] is from the same block of 
> memory as arr.

How does the compiler know which member of RCArray!int to check 
for pointing to the same memory chunk as val?


More information about the Digitalmars-d mailing list