Why dtor are not executed when removing a struct from associative arrays?
Tejas
notrealemail at gmail.com
Mon Sep 20 13:48:01 UTC 2021
On Monday, 20 September 2021 at 12:23:00 UTC, Learner wrote:
> I was expecting something like going out of scope for that
>
> ```(D)
> import std.stdio;
>
> struct S
> {
> ~this()
> {
> writeln("S is being destructed");
> }
> }
>
> void main()
> {
> S[int] aa;
> aa[1] = S();
> aa.remove(1);
> writeln("Why no dtor call on remove?");
> }
>
> I was expecting S instance dtor called
> S is being destructed
> ```
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.
More information about the Digitalmars-d-learn
mailing list