“Out” parameters and destructors
Ogion
ogion.art at gmail.com
Sat Mar 15 23:39:36 UTC 2025
On Saturday, 15 March 2025 at 20:21:41 UTC, Manfred Nowak wrote:
> If one moves the line containing 'writeln´ one line up before
> the 'if´-condition, then one might be notified of all
> calls---and one might be able to recognize more than one call
> of the destructor.
One who has eyes, let one see:
```D
import std.stdio;
struct S {
int x;
this(int x) {
this.x = x;
writeln("S(", x, ")");
}
~this() {
writeln("~S(", x, ")");
}
}
void foo(out S s) {}
void main() {
S s = S(42);
foo(s);
}
```
Output:
```
S(42)
~S(0)
```
Alas, the object instantiated with `S(42)` was not given the
proper burial. The treacherous function replaced its body with an
empty shell. Its restless spirit will wander the earth for all
eternity.
More information about the Digitalmars-d
mailing list