“Out” parameters and destructors
Ogion
ogion.art at gmail.com
Fri Mar 14 07:30:14 UTC 2025
Functions with `out` parameters simply initialize the parameter
with `.init`, instead of properly destroying it.
```D
import std.stdio;
int count;
struct S {
bool initialized;
this(int) {
writeln("S()");
initialized = true;
count++;
}
~this() {
if (initialized) {
writeln("~S()");
count--;
}
}
}
void foo(out S s) {
s = S(42);
}
void bar() {
S s;
foo(s);
foo(s);
}
void main() {
bar();
writeln(count);
}
```
Output:
```
S()
S()
~S()
1
```
Looks like a serious oversight.
More information about the Digitalmars-d
mailing list