Minimal druntime?

rikki cattermole rikki at cattermole.co.nz
Sat Jul 27 13:40:14 UTC 2019


On 28/07/2019 1:11 AM, Jonathan M Davis wrote:
> It sounds to me like what you want is -betterC.
> 
> - Jonathan M Davis


Indeed.

E.g.

extern(C) void main() {
     Foo foo = create!Foo;
     foo.func();
}

extern(C++) class Foo {
     int x;
     float y;

     this() {
     }

     this(int x) {
     	this.x = x;
     }

     void func() {
         import core.stdc.stdio : printf;

         printf("Hello!\n");
         printf("x = %d\n", x);
         printf("y = %f\n", y);
     }
}

T create(T, Args...)(Args args) {
     import core.stdc.stdlib : malloc;

     __gshared T Default = new T;
     enum Size = __traits(classInstanceSize, T);

     void* raw = cast(void*)Default;

     void* newRaw = malloc(Size);
     newRaw[0 .. Size] = raw[0 .. Size];

     T ret = cast(T)newRaw;

     static if (__traits(hasMember, T, "__ctor")) {
    	ret.__ctor(args);
     }

     return ret;
}


More information about the Digitalmars-d mailing list