digitalmars.D.learn - __dtor vs __xdtor
- bitwise (3/3) Aug 11 2017 What do they do?
- HyperParrow (4/7) Aug 11 2017 __xdtor() also calls the __dtor() that are mixed with template
- HyperParrow (20/28) Aug 11 2017 Nice example that speaks by itself:
- bitwise (3/20) Aug 11 2017 I think you mean assert(i == 1) for the second one, right?
- HyperParrow (4/25) Aug 11 2017 I made a mistake but it's not about i, which is a global.
- bitwise (7/15) Aug 11 2017 Ok thanks.
- HyperParrow (9/26) Aug 11 2017 Indeed. And there's also another one for the field called
- bitwise (4/15) Aug 11 2017 hmm...indeed ;)
- Marco Leise (13/20) Aug 12 2017 I think it was simply that all the special methods needed a
On Friday, 11 August 2017 at 16:53:02 UTC, bitwise wrote:What do they do? What's the difference? Thanks__xdtor() also calls the __dtor() that are mixed with template mixins while __dtor() only call the __dtor() that matches to the normal ~this(){}
Aug 11 2017
On Friday, 11 August 2017 at 17:02:20 UTC, HyperParrow wrote:On Friday, 11 August 2017 at 16:53:02 UTC, bitwise wrote:Nice example that speaks by itself: ================= int i; struct Foo { template ToMix(){ ~this(){++++i;}} ~this(){++i;} mixin ToMix; } void main() { Foo* foo = new Foo; foo.__xdtor; assert(i==3); Foo* other = new Foo; foo.__dtor; assert(i==4); // and not 6 ;) } =================What do they do? What's the difference? Thanks__xdtor() also calls the __dtor() that are mixed with template mixins while __dtor() only call the __dtor() that matches to the normal ~this(){}
Aug 11 2017
On Friday, 11 August 2017 at 17:06:40 UTC, HyperParrow wrote: [...]int i; struct Foo { template ToMix(){ ~this(){++++i;}} ~this(){++i;} mixin ToMix; } void main() { Foo* foo = new Foo; foo.__xdtor; assert(i==3); Foo* other = new Foo; foo.__dtor; assert(i==4); // and not 6 ;) } =================I think you mean assert(i == 1) for the second one, right?
Aug 11 2017
On Friday, 11 August 2017 at 17:12:22 UTC, bitwise wrote:On Friday, 11 August 2017 at 17:06:40 UTC, HyperParrow wrote: [...]I made a mistake but it's not about i, which is a global. I meant "other.__dtor." just before the last assert. This doesn't change the results.int i; struct Foo { template ToMix(){ ~this(){++++i;}} ~this(){++i;} mixin ToMix; } void main() { Foo* foo = new Foo; foo.__xdtor; assert(i==3); Foo* other = new Foo; foo.__dtor; assert(i==4); // and not 6 ;) } =================I think you mean assert(i == 1) for the second one, right?
Aug 11 2017
On Friday, 11 August 2017 at 17:02:20 UTC, HyperParrow wrote:On Friday, 11 August 2017 at 16:53:02 UTC, bitwise wrote:Ok thanks. I don't understand why you would ever want to call __dtor then...is it possible to have only __dtor without also having __xdtor? Like, if I want to call a struct's destructor, do I have to check for both, or can I just always check for, and call __xdtor?What do they do? What's the difference? Thanks__xdtor() also calls the __dtor() that are mixed with template mixins while __dtor() only call the __dtor() that matches to the normal ~this(){}
Aug 11 2017
On Friday, 11 August 2017 at 17:10:14 UTC, bitwise wrote:On Friday, 11 August 2017 at 17:02:20 UTC, HyperParrow wrote:Indeed. And there's also another one for the field called __fieldDtor.On Friday, 11 August 2017 at 16:53:02 UTC, bitwise wrote:Ok thanks. I don't understand why you would ever want to call __dtor then...What do they do? What's the difference? Thanks__xdtor() also calls the __dtor() that are mixed with template mixins while __dtor() only call the __dtor() that matches to the normal ~this(){}is it possible to have only __dtor without also having __xdtor? Like, if I want to call a struct's destructor, do I have to check for both, or can I just always check for, and call __xdtor?Always use __xdtor unless you know there's no other destructor to call. Actually these functions are an implementation details and should be only used for low level things like custom memory handling. You can look at core/lifetime.d to see how it's used by the d runtime.
Aug 11 2017
On Friday, 11 August 2017 at 17:20:18 UTC, HyperParrow wrote:[...] I made a mistake but it's not about i, which is a global. I meant "other.__dtor." just before the last assert. This doesn't change the results.hmm...indeed ;) On Friday, 11 August 2017 at 17:24:17 UTC, HyperParrow wrote:[...]Ok cool, thanks.is it possible to have only __dtor without also having __xdtor? Like, if I want to call a struct's destructor, do I have to check for both, or can I just always check for, and call __xdtor?Always use __xdtor unless you know there's no other destructor to call.
Aug 11 2017
Am Fri, 11 Aug 2017 17:10:14 +0000 schrieb bitwise <bitwise.pvt gmail.com>:Ok thanks. I don't understand why you would ever want to call __dtor then...is it possible to have only __dtor without also having __xdtor? Like, if I want to call a struct's destructor, do I have to check for both, or can I just always check for, and call __xdtor?I think it was simply that all the special methods needed a symbol name, so this() was called __ctor and ~this() was called __dtor. It was never supposed to cover field destruction, mixed in destructors or inheritance in classes. User code was not expected to call these directly anyways. Not very long ago __xdtor and __xpostblit were introduced that wrap up the entire finalization and copy operation. __dtor will remain as the 1:1 representation of the ~this() method. -- Marco
Aug 12 2017