digitalmars.D - betterC shared static ctor
- vit (11/11) Jul 21 2021 Is it possible to call all shared static ctors in betterC?
- vit (2/13) Jul 21 2021 Sorry, wrong forum section.
- bauss (5/16) Jul 21 2021 Static module constructors and destructors are not available with
- Steven Schveighoffer (16/31) Jul 21 2021 You can use [C runtime
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);
}
```
Jul 21 2021
On Wednesday, 21 July 2021 at 08:02:42 UTC, 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);
}
```
Sorry, wrong forum section.
Jul 21 2021
On Wednesday, 21 July 2021 at 08:02:42 UTC, 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);
}
```
Static module constructors and destructors are not available with
betterC unfortunately.
As described here:
https://dlang.org/spec/betterc.html#consequences
Jul 21 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
Jul 21 2021









vit <vit vit.vit> 