December 19, 2017 ptr wrapper with dip1000 | ||||
---|---|---|---|---|
| ||||
How to manualy declare constructor for struct Ptr which work like Ptr.create? struct Ptr{ int* ptr; static Ptr create(scope return int* ptr)@safe{ Ptr x; x.ptr = ptr; return x; } /++ This doesn't work: this(scope return int* ptr)scope @safe{ this.ptr = ptr; } +/ } void main()@safe{ int i; auto x = Ptr(&i); auto y = Ptr.create(&i); } |
December 19, 2017 Re: ptr wrapper with dip1000 | ||||
---|---|---|---|---|
| ||||
Posted in reply to vit | On 12/19/17 8:22 AM, vit wrote:
> struct Ptr{
> int* ptr;
>
> static Ptr create(scope return int* ptr)@safe{
> Ptr x;
> x.ptr = ptr;
> return x;
> }
>
> /++ This doesn't work:
> this(scope return int* ptr)scope @safe{
> this.ptr = ptr;
> }
> +/
> }
>
> void main()@safe{
> int i;
> auto x = Ptr(&i);
> auto y = Ptr.create(&i);
> }
I think this is a limitation of dip1000 that was not foreseen. I think when you mark the parameter `return`, `this` isn't actually considered a return value, is it? So it doesn't link the lifetime of ptr to the lifetime of this, which it should.
Please file an enhancement request, this should be allowed.
-Steve
|
Copyright © 1999-2021 by the D Language Foundation