opCast in class prevents destroy

cc cc at nevernet.com
Tue Mar 1 04:29:56 UTC 2022


```d
struct A {}
class B {
	A opCast(T : A)() {
		return A();
	}
}
void main() {
	auto b = new B();
	destroy(b);
}
```
fails with
```
dmd2\windows\bin\..\..\src\druntime\import\object.d(4209): Error: 
template instance `opCast!(void*)` does not match template 
declaration `opCast(T : A)()`
main.d(9): Error: template instance `object.destroy!(true, B)` 
error instantiating
```

Looks like a similar bug has been reported: 
https://issues.dlang.org/show_bug.cgi?id=22635

As a workaround, adding an additional opCast:
```d
class B {
	A opCast(T : A)() {
		return A();
	}
	auto opCast(T)() {
		return cast(T)super;
	}
}
```
SEEMS to work.  Is that safe?  Or are consequences not what I'm 
intending?


More information about the Digitalmars-d-learn mailing list