Feature: `static cast`

Nick Treleaven nick at geany.org
Sat Jun 3 12:59:18 UTC 2023


On Friday, 2 June 2023 at 17:53:41 UTC, Paul Backus wrote:
> Until the bug is fixed, casts like these should probably be a 
> compile-time error ("Error: dynamic casting of C++ classes is 
> not implemented"). If you want the current behavior, which is a 
> reinterpreting cast, the normal way to write that in D is with 
> a pointer cast like `*cast(D*) &c` (which is, appropriately, 
> `@system`).

Or `cast(D)cast(void*)c`.

> Of course, this would be a fairly disruptive breaking change, 
> so it would require a deprecation period, but I think it would 
> be worth it to disarm such an obvious footgun.

Essentially implemented that (but haven't fixed dmd etc yet with 
workaround):
https://github.com/dlang/dmd/pull/15293

A less disruptive change could be just to mark a derived cast as 
unsafe:
https://github.com/dlang/dmd/pull/15294


More information about the Digitalmars-d mailing list