CTFE limitation on reinterpreting cast

Walter Bright newshound2 at digitalmars.com
Wed Sep 30 00:17:25 UTC 2026


On 9/28/2026 3:50 PM, Rambutan wrote:
> struct B {}
> struct C {}
> 
> __gshared B Bobj;
> 
> auto ptr = cast(C*)&Bobj;

CTFE does not do anything that is not memory safe. Consider:

```d
struct B { int b; }
struct C { long c; }

__gshared B Bobj;

auto ptr = cast(C*)&Bobj;
```

This fails, because the structs are laid out differently, and `ptr.c` will 
produce memory unsafe behavior.

You could make the point that if the layouts of B and C are identical, this 
would be memory safe and should work.

Feel free to make the case for it!


More information about the Digitalmars-d mailing list