CTFE limitation on reinterpreting cast
monkyyy
crazymonkyyy at gmail.com
Mon Sep 28 22:56:52 UTC 2026
On Monday, 28 September 2026 at 22:50:21 UTC, Rambutan wrote:
> Here's a tiny version of something I ran into working on a
> project with both C and D source, while moving into D a C
> global array of structs that was initialized in place.
> ```
> struct B {}
> struct C {}
>
> __gshared B Bobj;
>
> auto ptr = cast(C*)&Bobj;
>
> void main() {}
> ```
> Without the cast the initializer `&Bobj` is fine, but with it
> this does not compile.
> ```
> $ gdc ctfecast.d
> ctfecast.d:6:20: error: reinterpreting cast from 'B*' to 'C*'
> is not supported in CTFE
> 6 | auto ptr = cast(C*)&Bobj;
> | ^
> ```
> Why? What can you tell me about this? Is there a workaround?
> Can you point me at connected documentation?
>
> Any information about this will be gratefully received.
>
> R.
initing is at compile time you want static this()
```d
struct B {}
struct C {}
__gshared B Bobj;
C* ptr;// = cast(C*)&Bobj;
static this(){
ptr=cast(C*)&Bobj;
}
void main() {}
```
---
Since its ct and therefore ctfe, the error your getting is from
the colored gc of the ctfe, its largely undocumented but its
mostly implemented ~~to avoid work on bit logic~~ when someone
could imagine how it would compile different on different
architectures
More information about the Digitalmars-d
mailing list