Return value storage classes - `scope` as well as `ref`

Dukc ajieskola at gmail.com
Tue May 30 07:21:44 UTC 2023


On Monday, 29 May 2023 at 10:08:38 UTC, Nick Treleaven wrote:
> And for `S.fp` here:
> https://forum.dlang.org/post/mqyqlieghqwsinbqiqbp@forum.dlang.org
>
> ```d
>     int* p;
>     {
>         auto lf = S(5);
>         p = lf.fp; // should error
>         // destructor called
>     }
> ```

No, this is working just as it should. `lf` is not `scope`, so a 
return value of a `return scope` function should be neither.

Maybe you're confusing lifetime of struct itself with lifetime of 
what it's pointing to. `return scope` return value is `scope` 
only if the struct is `scope` - that is, potentially pointing to 
a local with one or more of it's fields. Whether the struct 
itself is in stack is irrelevant.

On the other hand, `return` (without the trailing `scope`, 
`return ref` when applied to a parameter) means the return value 
is `scope` if the struct is in a stack. Maybe that's what you 
intended  `S.fp` to be.


More information about the Digitalmars-d mailing list