www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - betterC shared static ctor

reply vit <vit vit.vit> writes:
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
next sibling parent vit <vit vit.vit> writes:
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
prev sibling next sibling parent bauss <jj_1337 live.dk> writes:
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
prev sibling parent Steven Schveighoffer <schveiguy gmail.com> writes:
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