pointer escaping return scope bug?

Nick Treleaven nick at geany.org
Thu Dec 15 20:02:38 UTC 2022


On Saturday, 19 November 2022 at 15:24:33 UTC, Dukc wrote:
> On Saturday, 19 November 2022 at 15:02:54 UTC, Nick Treleaven 
> wrote:
>> OK, so how do I make `lf` implicitly scope?
>
> Have the `int*` inside it to point to a local, or assign 
> another `scope int*` to it.

Thanks, this works:

```d
@safe:

struct S
{
     int* p;
     int[0] v; // dummy storage
     auto f() return @trusted => p ? p : v.ptr;
}

void main()
{
     int* p;
     {
         S s = S(new int);
         p = s.f; // error
     }
}
```


More information about the Digitalmars-d-learn mailing list