www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - 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 2021
parent reply Mike Parker <aldacron gmail.com> writes:
On Wednesday, 21 July 2021 at 08:11:06 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);

 }
 ```
These rely on DRuntime, which is not linked in betterC mode. You'll need to use the `crt_constructor` and `crt_destructor` pragmas: https://dlang.org/spec/pragma.html#crtctor Apply them to any `extern(C) function for static ctor/dtor behavior.
Jul 21 2021
parent reply vit <vit vit.vit> writes:
On Wednesday, 21 July 2021 at 08:28:22 UTC, Mike Parker wrote:
 On Wednesday, 21 July 2021 at 08:11:06 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);

 }
 ```
These rely on DRuntime, which is not linked in betterC mode. You'll need to use the `crt_constructor` and `crt_destructor` pragmas: https://dlang.org/spec/pragma.html#crtctor Apply them to any `extern(C) function for static ctor/dtor behavior.
Thanks, it works, but now I have different problem. I need call static method for all instantions of template struct from `crt_constructor`. Is there way to iterate over all instantions of template? I need this to work but for all instantions of Foo, not only for Foo!1 a Foo!2 https://run.dlang.io/is/FNqHWh : ```d pragma(crt_constructor) extern(C)void shared_static_this(){ Foo!1.vtable_init(); Foo!2.vtable_init(); //Foo!3.vtable_init(); } extern(C) void main(){ auto foo1 = Foo!1(null); auto foo2 = Foo!2(null); auto foo3 = Foo!3(null); Base* b1 = &foo1.base; Base* b2 = &foo2.base; Base* b3 = &foo3.base; assert(b1.getId() == 1); assert(b2.getId() == 2); //assert(b3.getId() == 3); //vtable is not initialized } struct Vtable{ size_t offset; size_t function(void* ) getId; } struct Base{ immutable Vtable* vptr; this(immutable Vtable* vptr){ assert(vptr !is null); this.vptr = vptr; } size_t getId(){ return vptr.getId((cast(void*)&this) - vptr.offset); } } struct Foo(size_t id){ static immutable Vtable vtable; Base base; this(typeof(null) nil){ this.base = Base(&vtable); } static size_t virtual_getId(Foo* foo){ return id; } static void vtable_init(){ Vtable* vtable = cast(Vtable*)&vtable; vtable.offset = base.offsetof; vtable.getId = cast(typeof(Vtable.getId))&virtual_getId; } } ```
Jul 21 2021
parent Steven Schveighoffer <schveiguy gmail.com> writes:
On 7/21/21 5:07 AM, vit wrote:

 Thanks, it works, but now I have different problem.
 I need call static method for all instantions of template struct from 
 `crt_constructor`.
 Is there way to iterate over all instantions of template?
Not unless you register them somehow upon instantiation. Or (I think) you can implement the constructor as a static method, and tag it with `pragma(crt_constructor)`. Then it gets added when instantiated. -Steve
Jul 21 2021