constant pointer failing to compile

Steven Schveighoffer schveiguy at gmail.com
Thu Apr 6 01:06:46 UTC 2023


On 4/5/23 8:59 PM, Mathias LANG wrote:
>> immutable ubyte[4] data = [1, 2, 3, 4];
> 
> Using a static array instead of a slice will do the trick. You can leave 
> the `__gshared` if you want, but it is redundant on a global, 
> initialized `immutable` variable.

I found out the same thing. But I don't really understand why.

the error message gives a slight clue:

```
cannot use non-constant CTFE pointer in an initializer ‘&[cast(ubyte)1u, 
cast(ubyte)2u, cast(ubyte)3u, cast(ubyte)4u][0]’
```

That *isn't* a CTFE pointer, it's a pointer to a static immutable. Yes, 
the initialization value is a CTFE array, but the variable itself has an 
address. Notice how the expression has turned into an address of the 
initializer.

I think this is a compiler bug.

-Steve


More information about the Digitalmars-d-learn mailing list