Why dtor are not executed when removing a struct from associative arrays?

Tejas notrealemail at gmail.com
Mon Sep 20 14:03:09 UTC 2021


On Monday, 20 September 2021 at 13:48:01 UTC, Tejas wrote:
> On Monday, 20 September 2021 at 12:23:00 UTC, Learner wrote:
>> [...]
>
> I think it *is* being called:
> ```d
> import std.stdio;
>
> struct S
> {
> 	int a;
> 	this(int param){
> 		a = param;
> 	}
> 	~this()
> 	{
> 	    writeln("S(",a,") is being destructed");
> 	}
> }
>
> void main()
> {
> 	S[int] aa;
>         aa[1] = S(5);
>         aa.remove(1);
>         aa[1] = S(10);
>         //writeln("Why no dtor call on remove?");
> }
>
> Output:
> S(5) is being destructed
> S(10) is being destructed
> ```
> If  the destructors were being executed based on the ending of 
> scope, `S(10)` would have been destroyed first.

Oh dear, I made a mistake :(
Sorry, shouldn't have commented out the `writeln`


More information about the Digitalmars-d-learn mailing list