scope parameter has effect only on pointers?

Richard (Rikki) Andrew Cattermole richard at cattermole.co.nz
Sun Jun 22 15:53:45 UTC 2025


On 23/06/2025 3:08 AM, Neto wrote:
> this give the error:
> 
> ```d
> @safe:
> int *gp2;
> int g(scope int *p)
> {
>      gp2 = p;
>      return -1;
> }
> ```
> 
>>  Error: scope variable p assigned to non-scope gp2
> 
> but this one doesn't give any error:
> 
> ```d
> @safe:
> 
> int gg;
> 
> int f(scope int c)
> {
>      int k = c;
>      gg = c;
>      return k * 8;
> }
> ```
> 
> this is why `c` is a "simple variable" can be allocated onto stack and 
> copied without any issue? if not, why exactly this one doesn't give error?

An int is a basic type, by itself it can live in a register and does not 
have a unique memory address.

The location its in on the stack is irrelevant unless you take a pointer 
to it. In essence its a implementation detail.

It'll move between locations freely, this is why scope doesn't affect 
it. Because there is no resource to protect.



More information about the Digitalmars-d-learn mailing list