betterC shared static ctor
Steven Schveighoffer
schveiguy at gmail.com
Wed Jul 21 13:12:31 UTC 2021
On 7/21/21 4:02 AM, vit wrote:
> Is it possible to call all shared static ctors in betterC?
> ```d
> //-betterC
>
> static immutable int i;
>
> shared static this(){
> i = 42;
> }
> extern(C) void main(){
> assert(i != 42);
>
> }
>
> ```
You can use [C runtime
constructors](https://dlang.org/spec/pragma.html#crtctor). However, the
compiler doesn't recognize this as a static ctor exactly, so `i` will
not be mutable without casting.
```d
immutable int i;
extern(C):
pragma(crt_constructor) void crt_this() {
*cast(int *)&i = 42;
}
void main() {
assert(i == 42);
}
```
-Steve
More information about the Digitalmars-d
mailing list