D is about to take a wrong turn with interface identities

Quirin Schroll qs.il.paperinik at gmail.com
Tue Dec 12 11:17:32 UTC 2023


On Monday, 27 November 2023 at 12:08:51 UTC, Kagamin wrote:
> It's not obvious why `cast(void*)` should cast to Object. Any 
> application that requires accurate pointers would fail horribly.

Well, C++ does it, basically. D currently doesn’t support C++ 
RTTI, but it should not make decisions that will make it 
impossible to do so. For an `extern(C++)` interface, how else 
would you test for identity?

As has been pointed out to me, “accurate pointers,” i.e. type 
paint, cannot be generically expected to work using `cast(void*)` 
because of `opCast`. Use:

```d
R reinterpretCast(R, T)(auto ref T t)
{
     *cast(R*)cast(void**)&t
}
```

If anything, `cast(void*)` should do the exact same thing as 
[`dynamic_cast<void*>`](https://en.cppreference.com/w/cpp/language/dynamic_cast) does in C++, at least for `extern(C++)` classes, and if we want to be consistent – and there’s no reason not to –, it should do the same also for `extern(D)` classes.


More information about the Digitalmars-d mailing list